-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathutils.c
49 lines (40 loc) · 862 Bytes
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "utils.h"
/*
* Use this allocator if you have an object a child writes to that you want
* all other processes to see.
*/
void * alloc_shared(unsigned int size)
{
void *ret;
ret = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
if (ret == MAP_FAILED)
return NULL;
return ret;
}
void * zmalloc(size_t size)
{
void *p;
p = malloc(size);
if (p == NULL) {
printf("malloc(%zu) failure.\n", size);
exit(EXIT_FAILURE);
}
memset(p, 0, size);
return p;
}
void sizeunit(unsigned long size, char *buf)
{
if (size < 1024 * 1024) {
sprintf(buf, "%lu bytes", size);
return;
}
if (size < (1024 * 1024 * 1024)) {
sprintf(buf, "%ldMB", (size / 1024) / 1024);
return;
}
sprintf(buf, "%ldGB", ((size / 1024) / 1024) / 1024);
}