forked from tomdionysus/pmalloc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_suballocation.c
85 lines (64 loc) · 1.68 KB
/
example_suballocation.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pmalloc.h"
int main() {
printf("pmalloc: Suballocation Example\n\n");
const uint32_t MEMORY_SIZE = 1024*1024;
// Set up a megabyte of memory with the OS malloc
char *memory = malloc(MEMORY_SIZE);
if(memory == NULL) return 1;
// Our pmalloc struct
pmalloc_t pmm;
pmalloc_t *pm = &pmm;
pmalloc_init(pm);
pmalloc_addblock(pm, memory, MEMORY_SIZE);
printf("Allocating\n");
char *strs[] = {
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten"
};
char *ptrs[10];
for(uint8_t i=0; i<10; i++) {
uint8_t len = strlen(strs[i])+1;
printf("Allocating %d bytes for '%s', ", len, strs[i]);
ptrs[i] = (char*)pmalloc_malloc(pm, len);
printf("Copying, ");
strcpy(ptrs[i], strs[i]);
printf("Done.\n");
}
printf("Printing from Allocations\n");
for(uint8_t i=0; i<10; i++) {
printf("'%s' is length %d\n", ptrs[i], pmalloc_sizeof(pm, ptrs[i]));
}
printf("Removing 4 and 5\n");
pmalloc_free(pm, ptrs[3]);
pmalloc_free(pm, ptrs[4]);
ptrs[3] = NULL;
ptrs[4] = NULL;
printf("Reallocating 'Eleven'\n");
char *eleven = "Eleven";
uint8_t len = strlen(eleven);
printf("Allocating %d bytes for '%s', ", len, eleven);
ptrs[3] = (char*)pmalloc_malloc(pm, len);
printf("Copying, ");
strcpy(ptrs[3], eleven);
printf("Done.\n");
printf("Printing from Allocations\n");
for(uint8_t i=0; i<10; i++) {
if(ptrs[i]==NULL) continue;
printf("'%s' is length %d\n", ptrs[i], pmalloc_sizeof(pm, ptrs[i]));
}
// Note we don't have to pmalloc_free our allocations here - we're just dropping the entire memory back to the OS.
// Free the OS Memory
free(memory);
return 0;
}