Created
April 29, 2014 18:28
-
-
Save bdw/11408247 to your computer and use it in GitHub Desktop.
Proposed 'pointer buffer' implementation - pointer buffers are extremely common in moarvm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
struct MVMPointerBuffer { | |
union { | |
void ** v; | |
MVMObject **objects; | |
MVMCollectable **collectables; | |
MVMSpeshBB **spesh_bbs; | |
/* some other common pointer types */ | |
} p; | |
MVMint32 num; | |
MMVint32 alloc; | |
}; | |
typedef struct MVMPointerBuffer MVMPointerBuffer; | |
void MVM_pb_init(MVMPointerBuffer * buffer, MVMint32 initial_size) { | |
buffer->p.v = calloc(initial_size, sizeof(void*)); | |
buffer->num = 0; | |
buffer->alloc = initial_size; | |
} | |
void MVM_pb_append(MVMPointerBuffer * buffer, void * item) { | |
if (buffer->num >= buffer->alloc) { | |
buffer->alloc *= 2; | |
buffer->p.v = realloc(buffer->p.v, buffer->alloc * sizeof(void*)); | |
} | |
buffer->p.v[buffer->num] = item; | |
buffer->num++; | |
} | |
void MVM_pb_destroy(MVMPointerBuffer * buffer) { | |
free(buffer->p.v); | |
buffer->alloc = 0; | |
buffer->num = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment