summaryrefslogtreecommitdiff
path: root/src/lib/memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/memory.c')
-rw-r--r--src/lib/memory.c84
1 files changed, 27 insertions, 57 deletions
diff --git a/src/lib/memory.c b/src/lib/memory.c
index fbc0af7..b5bb82a 100644
--- a/src/lib/memory.c
+++ b/src/lib/memory.c
@@ -46,7 +46,7 @@ memory_init(struct aplwc *aplwc)
aplwc->unaligned_data = NULL;
aplwc->size = 0;
aplwc->memptrs = NULL;
- aplwc->rootptrs = NULL;
+ aplwc->refs = NULL;
aplwc->reachable_bit = 0;
}
@@ -106,9 +106,9 @@ next(struct chunk *c)
for(i = 0; i < frame->count; i++) \
if(frame->ptrs[i]) \
X(*frame->ptrs[i]); \
- for(root = aplwc->rootptrs; root; root = root->link) \
- if(root->ptr) \
- X(root->ptr); \
+ for(ref = aplwc->refs; ref; ref = ref->link) \
+ if(ref->ptr) \
+ X(ref->ptr); \
}while(0)
#define VISIT_ALL_PTRS(X, data) do{ \
@@ -123,7 +123,7 @@ fixrefs(struct aplwc *aplwc, uint8_t *new, uint8_t *old)
struct chunk *c;
struct memory_ptr_frame *frame;
- struct root_ptr *root;
+ struct aplwc_ref *ref;
size_t add, sub, i;
if(new > old){
@@ -263,7 +263,7 @@ collect(struct aplwc *aplwc)
struct chunk *c, *prev;
struct chunk *stack;
struct memory_ptr_frame *frame;
- struct root_ptr *root;
+ struct aplwc_ref *ref;
size_t i;
aplwc->reachable_bit = ~aplwc->reachable_bit;
@@ -300,7 +300,7 @@ swapptr(struct aplwc *aplwc, void *old, void *new)
{
struct chunk *c;
struct memory_ptr_frame *frame;
- struct root_ptr *root;
+ struct aplwc_ref *ref;
size_t i;
#define SWAP(ptr) do{ \
@@ -427,71 +427,40 @@ retry:
return data(new);
}
-struct aplwc_ref
-memory_root_new(struct aplwc *aplwc, void *p)
+void
+memory_root_new(struct aplwc *aplwc, void *p, struct aplwc_ref *ref)
{
- struct aplwc_ref ref;
- struct root_ptr *root;
- size_t size;
-
- clear_ref(&ref);
- if(aplwc->fns.alloc == NULL){
- aplwc->last_error = APLWC_OUT_OF_MEMORY;
- goto end;
- }
-
- root = aplwc->fns.alloc(sizeof(*root), sizeof(*root), &size);
- if(root == NULL){
- aplwc->last_error = APLWC_OUT_OF_MEMORY;
- goto end;
- }
-
- root->id = aplwc->rootptrs ? (aplwc->rootptrs->id+1) : 0;
- root->ptr = p;
- root->link = aplwc->rootptrs;
- aplwc->rootptrs = root;
-
- ref.id = root->id;
- ref.aplwc = aplwc;
-end:
- return ref;
+ ref->ptr = p;
+ ref->link = aplwc->refs;
+ ref->aplwc = aplwc;
+ aplwc->refs = ref;
}
void *
-memory_root_get(struct aplwc_ref ref)
+memory_root_get(struct aplwc_ref *ref)
{
- struct root_ptr *root;
-
- if(!valid_ref(ref))
- return NULL;
-
- for(root = ref.aplwc->rootptrs; root; root = root->link)
- if(root->id == ref.id)
- return root->ptr;
-
- return NULL;
+ return ref->ptr;
}
void
-memory_root_free(struct aplwc_ref ref)
+memory_root_free(struct aplwc_ref *ref)
{
struct aplwc *aplwc;
- struct root_ptr **root, *tmp;
+ struct aplwc_ref **root;
if(!valid_ref(ref))
return;
- aplwc = ref.aplwc;
+ aplwc = ref->aplwc;
- for(root = &aplwc->rootptrs; (*root); root = &(*root)->link){
- if((*root)->id == ref.id){
- tmp = *root;
- *root = tmp->link;
- if(aplwc->fns.dealloc)
- aplwc->fns.dealloc(tmp);
+ for(root = &aplwc->refs; (*root); root = &(*root)->link){
+ if((*root) == ref){
+ *root = (*root)->link;
break;
}
}
+
+ clear_ref(ref);
}
void
@@ -564,12 +533,13 @@ memory_alloc_aplwc_module(struct aplwc *aplwc)
void
clear_ref(struct aplwc_ref *ref)
{
- ref->id = -1;
ref->aplwc = NULL;
+ ref->ptr = NULL;
+ ref->link = NULL;
}
int
-valid_ref(struct aplwc_ref ref)
+valid_ref(struct aplwc_ref *ref)
{
- return ref.id >= 0;
+ return (ref->aplwc != NULL) && (ref->ptr != NULL);
}