source: doc/theses/rob_schluntz_MMath/examples/ctor/placement_ctor.c

Last change on this file was 67982887, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

specialize thesis directory-names

  • 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.