Browse Source

Remove more dynamic allocation

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
b6b36e8ac4
1 changed files with 20 additions and 8 deletions
  1. 20
    8
      src/hci/mucurses/slk.c

+ 20
- 8
src/hci/mucurses/slk.c View File

2
 #include <stddef.h>
2
 #include <stddef.h>
3
 #include <malloc.h>
3
 #include <malloc.h>
4
 #include <string.h>
4
 #include <string.h>
5
+#include <assert.h>
5
 #include "mucurses.h"
6
 #include "mucurses.h"
6
 
7
 
7
 /** @file
8
 /** @file
11
 
12
 
12
 #define MIN_SPACE_SIZE 2
13
 #define MIN_SPACE_SIZE 2
13
 
14
 
15
+#define SLK_MAX_LABEL_LEN 8
16
+
17
+#define SLK_MAX_NUM_LABELS 12
18
+
19
+#define SLK_MAX_NUM_SPACES 2
20
+
14
 struct _softlabel {
21
 struct _softlabel {
15
 	// label string
22
 	// label string
16
-	char *label;
23
+	char label[SLK_MAX_LABEL_LEN];
17
 	/* Format of soft label 
24
 	/* Format of soft label 
18
 	   0: left justify
25
 	   0: left justify
19
 	   1: centre justify
26
 	   1: centre justify
23
 };
30
 };
24
 
31
 
25
 struct _softlabelkeys {
32
 struct _softlabelkeys {
26
-	struct _softlabel *fkeys;
33
+	struct _softlabel fkeys[SLK_MAX_NUM_LABELS];
27
 	attr_t attrs;
34
 	attr_t attrs;
28
 	/* Soft label layout format
35
 	/* Soft label layout format
29
 	   0: 3-2-3
36
 	   0: 3-2-3
36
 	unsigned int maj_space_len;
43
 	unsigned int maj_space_len;
37
 	unsigned int num_labels;
44
 	unsigned int num_labels;
38
 	unsigned int num_spaces;
45
 	unsigned int num_spaces;
39
-	unsigned int spaces[2];
46
+	unsigned int spaces[SLK_MAX_NUM_SPACES];
40
 };
47
 };
41
 
48
 
42
 struct _softlabelkeys *slks;
49
 struct _softlabelkeys *slks;
55
 static void _print_label ( struct _softlabel sl ) {
62
 static void _print_label ( struct _softlabel sl ) {
56
 	unsigned short i = 0;
63
 	unsigned short i = 0;
57
 	int space_ch;
64
 	int space_ch;
58
-	char *str = malloc((size_t)slks->max_label_len);
65
+	char str[SLK_MAX_LABEL_LEN + 1];
59
 
66
 
67
+	assert ( slks->max_label_len <= SLK_MAX_LABEL_LEN );
60
 	space_ch = ' ';
68
 	space_ch = ' ';
61
 
69
 
62
 	// protect against gaps in the soft label keys array
70
 	// protect against gaps in the soft label keys array
230
 		return ERR;
238
 		return ERR;
231
 	}
239
 	}
232
 
240
 
233
-	slks = malloc(sizeof(struct _softlabelkeys));
241
+	/* There seems to be no API call to free this data structure... */
242
+	if ( ! slks )
243
+		slks = calloc(1,sizeof(*slks));
244
+	if ( ! slks )
245
+		return ERR;
246
+
234
 	slks->attrs = A_DEFAULT;
247
 	slks->attrs = A_DEFAULT;
235
 	slks->fmt = fmt;
248
 	slks->fmt = fmt;
236
 	switch(fmt) {
249
 	switch(fmt) {
260
 		( available_width % nblocks ) / nmaj;
273
 		( available_width % nblocks ) / nmaj;
261
 	slks->num_spaces = nmaj;
274
 	slks->num_spaces = nmaj;
262
 	slks->num_labels = nblocks;
275
 	slks->num_labels = nblocks;
263
-	slks->fkeys = calloc( nblocks, sizeof(struct _softlabel) );
264
 
276
 
265
 	// strip a line from the screen
277
 	// strip a line from the screen
266
 	LINES -= 1;
278
 	LINES -= 1;
337
 	if ( (unsigned short)fmt >= 3 )
349
 	if ( (unsigned short)fmt >= 3 )
338
 		return ERR;
350
 		return ERR;
339
 
351
 
340
-	slks->fkeys[labnum].label = malloc((size_t)slks->max_label_len + 1);
341
-	strncpy(slks->fkeys[labnum].label, label, (size_t)slks->max_label_len);
352
+	strncpy(slks->fkeys[labnum].label, label,
353
+		sizeof(slks->fkeys[labnum].label));
342
 	slks->fkeys[labnum].fmt = fmt;
354
 	slks->fkeys[labnum].fmt = fmt;
343
 
355
 
344
 	return OK;
356
 	return OK;

Loading…
Cancel
Save