source: src/examples/gc_no_raii/src/vector.c@ 8c8b614

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory 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 8c8b614 was 385c130, checked in by Thierry Delisle <tdelisle@…>, 10 years ago

pre merge

  • Property mode set to 100644
File size: 1.7 KB
Line 
1#include "vector.h"
2
3//------------------------------------------------------------------------------
4//Initialization
5forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
6void vector_ctor(vector(T, allocator_t) *const this)
7{
8 ctor(&this->storage);
9 this->size = 0;
10}
11
12forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
13void dtor(vector(T, allocator_t) *const this)
14{
15 dtor(&this->storage);
16}
17
18//------------------------------------------------------------------------------
19//Modifiers
20forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
21void push_back(vector(T, allocator_t) *const this, T value)
22{
23 realloc(&this->storage, this->size+1);
24 data(&this->storage)[this->size] = value;
25 this->size++;
26}
27
28forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
29void pop_back(vector(T, allocator_t) *const this)
30{
31 this->size--;
32 DESTROY(data(&this->storage)[this->size]);
33}
34
35forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
36void clear(vector(T, allocator_t) *const this)
37{
38 for(size_t i = 0; i < this->size; i++)
39 {
40 DESTROY(data(&this->storage)[this->size]);
41 }
42 this->size = 0;
43}
44
45//------------------------------------------------------------------------------
46//Allocator
47forall(otype T)
48void ctor(heap_allocator(T) *const this)
49{
50 this->storage = 0;
51 this->capacity = 0;
52}
53
54forall(otype T)
55void dtor(heap_allocator(T) *const this)
56{
57 free((void*)this->storage);
58}
59
60forall(otype T)
61inline void realloc(heap_allocator(T) *const this, size_t size)
62{
63 static const size_t GROWTH_RATE = 2;
64 if(size > this->capacity)
65 {
66 this->capacity = GROWTH_RATE * size;
67 this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T));
68 }
69}
Note: See TracBrowser for help on using the repository browser.