Browse Source

Remove more dynamic allocation

tags/v0.9.3
Michael Brown 17 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,6 +2,7 @@
2 2
 #include <stddef.h>
3 3
 #include <malloc.h>
4 4
 #include <string.h>
5
+#include <assert.h>
5 6
 #include "mucurses.h"
6 7
 
7 8
 /** @file
@@ -11,9 +12,15 @@
11 12
 
12 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 21
 struct _softlabel {
15 22
 	// label string
16
-	char *label;
23
+	char label[SLK_MAX_LABEL_LEN];
17 24
 	/* Format of soft label 
18 25
 	   0: left justify
19 26
 	   1: centre justify
@@ -23,7 +30,7 @@ struct _softlabel {
23 30
 };
24 31
 
25 32
 struct _softlabelkeys {
26
-	struct _softlabel *fkeys;
33
+	struct _softlabel fkeys[SLK_MAX_NUM_LABELS];
27 34
 	attr_t attrs;
28 35
 	/* Soft label layout format
29 36
 	   0: 3-2-3
@@ -36,7 +43,7 @@ struct _softlabelkeys {
36 43
 	unsigned int maj_space_len;
37 44
 	unsigned int num_labels;
38 45
 	unsigned int num_spaces;
39
-	unsigned int spaces[2];
46
+	unsigned int spaces[SLK_MAX_NUM_SPACES];
40 47
 };
41 48
 
42 49
 struct _softlabelkeys *slks;
@@ -55,8 +62,9 @@ static void _movetoslk ( void ) {
55 62
 static void _print_label ( struct _softlabel sl ) {
56 63
 	unsigned short i = 0;
57 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 68
 	space_ch = ' ';
61 69
 
62 70
 	// protect against gaps in the soft label keys array
@@ -230,7 +238,12 @@ int slk_init ( int fmt ) {
230 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 247
 	slks->attrs = A_DEFAULT;
235 248
 	slks->fmt = fmt;
236 249
 	switch(fmt) {
@@ -260,7 +273,6 @@ int slk_init ( int fmt ) {
260 273
 		( available_width % nblocks ) / nmaj;
261 274
 	slks->num_spaces = nmaj;
262 275
 	slks->num_labels = nblocks;
263
-	slks->fkeys = calloc( nblocks, sizeof(struct _softlabel) );
264 276
 
265 277
 	// strip a line from the screen
266 278
 	LINES -= 1;
@@ -337,8 +349,8 @@ int slk_set ( int labnum, const char *label, int fmt ) {
337 349
 	if ( (unsigned short)fmt >= 3 )
338 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 354
 	slks->fkeys[labnum].fmt = fmt;
343 355
 
344 356
 	return OK;

Loading…
Cancel
Save