121 lines
2.7 KiB
C
121 lines
2.7 KiB
C
#include "VoidMan.h"
|
|
#include <stdlib.h>
|
|
|
|
/* VoidMan ================================================ */
|
|
// creation ================================
|
|
struct VoidMan *newVoidMan(int size) {
|
|
struct VoidMan *vman = malloc(sizeof(struct VoidMan));
|
|
initVoidMan(vman, size);
|
|
return vman;
|
|
}
|
|
struct VoidMan *freeVoidMan(struct VoidMan *vman) {
|
|
int i;
|
|
for (i = 0; i < vman->index.size; i++) {
|
|
if (vman->index.ids[i] == 1) {
|
|
free(vman->object[i]);
|
|
}
|
|
}
|
|
clearIndex(&vman->index);
|
|
free(vman);
|
|
return NULL;
|
|
}
|
|
int clearVoidMan(struct VoidMan *vman) {
|
|
int i;
|
|
for (i = 0; i < vman->index.size; i++) {
|
|
if (vman->index.ids[i] == 1) {
|
|
free(vman->object[i]);
|
|
}
|
|
}
|
|
free(vman->object);
|
|
vman->iter = 0;
|
|
vman->count = 0;
|
|
clearIndex(&vman->index);
|
|
return 0;
|
|
}
|
|
|
|
int initVoidMan(struct VoidMan *vman, int size) {
|
|
vman->iter = 0;
|
|
vman->count = 0;
|
|
initIndex(&vman->index, size);
|
|
vman->object = malloc(size*(sizeof(void*)));
|
|
return 0;
|
|
}
|
|
|
|
int growVoidMan(struct VoidMan *vman, int amount) {
|
|
int old_size = vman->index.size;
|
|
int new_size = old_size + amount;
|
|
// grow our collection of objects
|
|
vman->object = realloc(vman->object, new_size*(sizeof(void*)));
|
|
// grow the index now
|
|
growIndex(&vman->index, amount);
|
|
return 0;
|
|
}
|
|
|
|
/* Object add, delete, and access ================================ */
|
|
int addObject(struct VoidMan *vman, void *object) {
|
|
int id;
|
|
// FIXME: this is unsafe, as it could go infinite!
|
|
while((id = acquireIndex(&vman->index)) < 0) {
|
|
growVoidMan(vman, 8);
|
|
}
|
|
vman->object[id] = object;
|
|
addIndex(&vman->index, id);
|
|
vman->count++;
|
|
return id;
|
|
}
|
|
int delObject(struct VoidMan *vman, int id) {
|
|
if (vman->index.ids[id] == 1) {
|
|
vman->index.ids[id] = 0;
|
|
free(vman->object[id]);
|
|
vman->object[id] = NULL;
|
|
vman->count--;
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
int remObject(struct VoidMan *vman, int id) {
|
|
if (vman->index.ids[id] == 1) {
|
|
vman->index.ids[id] = 0;
|
|
vman->object[id] = NULL;
|
|
vman->count--;
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int getObjecti(struct VoidMan *vman) {
|
|
int i;
|
|
for (i = 0; i < vman->index.size; i++) {
|
|
if (vman->index.ids[i] == 1) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void *iterObject(struct VoidMan *vman) {
|
|
int i;
|
|
for (i = vman->iter; i < vman->index.size; i++) {
|
|
if (vman->index.ids[i] == 1) {
|
|
vman->iter = i+1;
|
|
return vman->object[i];
|
|
}
|
|
}
|
|
// if we got here, that means we couldn't find any objects
|
|
vman->iter = 0;
|
|
return NULL;
|
|
}
|
|
|
|
int iterObjecti(struct VoidMan *vman) {
|
|
int i;
|
|
for (i = vman->iter; i < vman->index.size; i++) {
|
|
if (vman->index.ids[i] == 1) {
|
|
vman->iter = i+1;
|
|
return i;
|
|
}
|
|
}
|
|
// if we got here, that means we couldn't find any objects
|
|
vman->iter = 0;
|
|
return -1;
|
|
}
|