note a leak that needs fixing eventually
[trinity.git] / utils.c
blob5fc825ff41a9fec480e3b3cd3c9040583030f459
1 #include <sys/mman.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "utils.h"
8 /*
9 * Use this allocator if you have an object a child writes to that you want
10 * all other processes to see.
12 void * alloc_shared(unsigned int size)
14 void *ret;
16 ret = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
17 if (ret == MAP_FAILED) {
18 printf("mmap %u failure\n", size);
19 exit(EXIT_FAILURE);
21 return ret;
24 void * __zmalloc(size_t size, const char *func)
26 void *p;
28 p = malloc(size);
29 if (p == NULL) {
30 printf("%s: malloc(%zu) failure.\n", func, size);
31 exit(EXIT_FAILURE);
34 memset(p, 0, size);
35 return p;
38 void sizeunit(unsigned long size, char *buf)
40 if (size < 1024 * 1024) {
41 sprintf(buf, "%lu bytes", size);
42 return;
45 if (size < (1024 * 1024 * 1024)) {
46 sprintf(buf, "%ldMB", (size / 1024) / 1024);
47 return;
50 sprintf(buf, "%ldGB", ((size / 1024) / 1024) / 1024);