-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalloc.c
More file actions
63 lines (46 loc) · 1.38 KB
/
Copy pathalloc.c
File metadata and controls
63 lines (46 loc) · 1.38 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>
#include "alloc.h"
#include "sem.h"
#define ALLOC_STRICT_MEMORY_ENABLE 0
struct rk_sema semAlloc = {0};
struct c_countChanges countChanges;
void c_initSem() {
ALLOC_STRICT_MEMORY_ENABLE && rk_sema_init(&semAlloc, 1);
}
void *c_malloc(int size) {
ALLOC_STRICT_MEMORY_ENABLE && rk_sema_wait(&semAlloc);
countChanges.countMalloc++;
ALLOC_STRICT_MEMORY_ENABLE && rk_sema_post(&semAlloc);
void *result = malloc(size);
if (result == NULL) {
printf("Malloc failure, size = %d\n", size);
}
return result;
}
void *c_calloc(int count, int size) {
ALLOC_STRICT_MEMORY_ENABLE && rk_sema_wait(&semAlloc);
countChanges.countCalloc++;
ALLOC_STRICT_MEMORY_ENABLE && rk_sema_post(&semAlloc);
void *result = calloc(count, size);
if (result == NULL) {
printf("Calloc failure, count = %d, size = %d\n", count, size);
}
return result;
}
void c_free(void *pointer) {
ALLOC_STRICT_MEMORY_ENABLE && rk_sema_wait(&semAlloc);
countChanges.countFree++;
ALLOC_STRICT_MEMORY_ENABLE && rk_sema_post(&semAlloc);
free(pointer);
}
void *c_realloc(void *pointer, size_t size) {
void *result = realloc(pointer, size);
if(result == NULL) {
printf("Realloc failure, needSize = %zu\n", size);
}
return result;
}
struct c_countChanges *c_result() {
return &countChanges;
}