rename
[trinity.git] / utils.c
blob3615ac8611e759c39d5ae40d430afa12cb8bafc7
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)
26 void *p;
28 p = malloc(size);
29 if (p == NULL) {
30 printf("malloc(%zu) failure.\n", 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);