Skip to content

Instantly share code, notes, and snippets.

@bdw
Created April 29, 2014 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdw/11408247 to your computer and use it in GitHub Desktop.
Save bdw/11408247 to your computer and use it in GitHub Desktop.
Proposed 'pointer buffer' implementation - pointer buffers are extremely common in moarvm
#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