source: doc/rob_thesis/examples/ctor/placement_ctor.c@ 61255ad

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 61255ad was 9c14ae9, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

add thesis source

  • Property mode set to 100644
File size: 1.1 KB
Line 
1struct memory_pool {
2 char * start;
3 char * cur;
4 size_t size;
5 char * free;
6};
7
8void ?{}(memory_pool * pool, size_t size) {
9 pool->[start, cur] = malloc(size);
10 pool->size = size;
11 printf("initializing memory pool with size %lu at location %p\n", pool->size, pool->start);
12}
13
14void ^?{}(memory_pool * pool) {
15 free(pool->start);
16}
17
18forall(dtype T | sized(T))
19T * allocate(memory_pool * pool, unsigned int array_size = 1) {
20 size_t size = sizeof(T) * array_size;
21 printf("allocating block of size %lu...", size);
22 if (pool->cur + size < pool->start + pool->size) {
23 T * x = (T*)pool->cur;
24 pool->cur += size;
25 printf("success!\n");
26 printf("next address is %p\n", pool->cur);
27 return x;
28 } else {
29 printf("failed!\n");
30 // fail to allocate
31 return 0;
32 }
33}
34
35struct A {
36 int x, y, z;
37};
38void ?{}(A * a) {
39 a->[x,y,z] = [123, 456, 789];
40}
41
42int main() {
43 memory_pool pool = { 1024 };
44
45 int * x = allocate(&pool);
46 A * a = allocate(&pool);
47 A * b = allocate(&pool, 1000);
48 a{};
49 printf("%p\n", x);
50 printf("%p %d %d %d\n", a, a->[x,y,z]);
51}
Note: See TracBrowser for help on using the repository browser.