Jelajahi Sumber

use mallocx

David Marec 9 bulan lalu
induk
melakukan
59a85a1c9d
2 mengubah file dengan 12 tambahan dan 5 penghapusan
  1. 2 1
      sharedmemory/main.c
  2. 10 4
      testTimer/timerTest.c

+ 2 - 1
sharedmemory/main.c

@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>  //bzero
+#include <malloc_np.h>
 
 static const char* pathname="/tmp/rats";
 
@@ -139,7 +140,7 @@ static void showmem()
 			(uint32_t)curbuf[2] << 8  |
 			(uint32_t)curbuf[3];
 		if (l > 0) {
-			uint8_t* buf = malloc(l);
+			uint8_t *buf = mallocx(l, MALLOCX_ZERO);
 			if (buf) {
 				/* data */
 				ssize_t total = pread(fd, buf, l, 4);

+ 10 - 4
testTimer/timerTest.c

@@ -26,8 +26,8 @@ struct slisthead *headp;	/* Singly-linked List head. */
 struct entry {
 	char	       *name;
 	int		code;
-			SLIST_ENTRY(entry) entries;	/* Singly-linked List. */
-}	       *np;
+	SLIST_ENTRY(entry) entries;	/* Singly-linked List. */
+}   *np;
 
 /*
  * signal handler
@@ -72,9 +72,10 @@ int
 main(int argc, char *argv[])
 {
 	SLIST_INIT(&head);
-
 	/* timer pointers */
 	size_t		nb_timers = 5;
+	size_t		cols = 0;
+	
 
 	if (argc > 1) {
 		nb_timers = atoi(argv[1]);
@@ -96,7 +97,7 @@ main(int argc, char *argv[])
 			strncpy(names[i], ".", MAX_LEN_NAME);
 			continue;
 		}
-		if (MAX_LEN_NAME <= snprintf(names[i], MAX_LEN_NAME, "+%zu+", i * 2 + 1)) {
+		if (MAX_LEN_NAME <= snprintf(names[i], MAX_LEN_NAME, "%c", (char)('A' + i - 1))) {
 			warnx("string %zu shorten", i);
 		}
 	}
@@ -146,6 +147,10 @@ main(int argc, char *argv[])
 	printf("waiting...\n");
 	for (; !spankme;) {
 		pause();
+		if (++cols > 79) { 
+			cols = 0;
+			printf("\n");
+		}
 	}
 
 	printf("\nbye.\n");
@@ -160,5 +165,6 @@ main(int argc, char *argv[])
 		free(np);
 	}
 
+	return EXIT_SUCCESS;
 
 }