Remove zero special casing from named array

This commit is contained in:
Luke Benstead 2019-02-28 18:33:37 +00:00
parent 31ed419dfa
commit b0c9ac7207
2 changed files with 11 additions and 9 deletions

View File

@ -136,6 +136,9 @@ static GLuint _glGetMipmapDataSize(TextureObject* obj) {
GLubyte _glKosInitTextures() { GLubyte _glKosInitTextures() {
named_array_init(&TEXTURE_OBJECTS, sizeof(TextureObject), MAX_TEXTURE_COUNT); named_array_init(&TEXTURE_OBJECTS, sizeof(TextureObject), MAX_TEXTURE_COUNT);
// Reserve zero so that it is never given to anyone as an ID!
named_array_reserve(&TEXTURE_OBJECTS, 0);
SHARED_PALETTE = (TexturePalette*) malloc(sizeof(TexturePalette)); SHARED_PALETTE = (TexturePalette*) malloc(sizeof(TexturePalette));
return 1; return 1;
} }
@ -191,6 +194,9 @@ void APIENTRY glGenTextures(GLsizei n, GLuint *textures) {
while(n--) { while(n--) {
GLuint id = 0; GLuint id = 0;
TextureObject* txr = (TextureObject*) named_array_alloc(&TEXTURE_OBJECTS, &id); TextureObject* txr = (TextureObject*) named_array_alloc(&TEXTURE_OBJECTS, &id);
assert(id); // Generated IDs must never be zero
_glInitializeTextureObject(txr, id); _glInitializeTextureObject(txr, id);
*textures = id; *textures = id;

View File

@ -31,8 +31,6 @@ void named_array_init(NamedArray* array, unsigned int element_size, unsigned int
} }
char named_array_used(NamedArray* array, unsigned int id) { char named_array_used(NamedArray* array, unsigned int id) {
id--;
unsigned int i = id / 8; unsigned int i = id / 8;
unsigned int j = id % 8; unsigned int j = id % 8;
@ -44,11 +42,11 @@ void* named_array_alloc(NamedArray* array, unsigned int* new_id) {
unsigned int i = 0, j = 0; unsigned int i = 0, j = 0;
for(i = 0; i < array->marker_count; ++i) { for(i = 0; i < array->marker_count; ++i) {
for(j = 0; j < 8; ++j) { for(j = 0; j < 8; ++j) {
unsigned int id = (i * 8) + j + 1; unsigned int id = (i * 8) + j;
if(!named_array_used(array, id)) { if(!named_array_used(array, id)) {
array->used_markers[i] |= (unsigned char) 1 << j; array->used_markers[i] |= (unsigned char) 1 << j;
*new_id = id; *new_id = id;
unsigned char* ptr = &array->elements[(id - 1) * array->element_size]; unsigned char* ptr = &array->elements[id * array->element_size];
memset(ptr, 0, array->element_size); memset(ptr, 0, array->element_size);
return ptr; return ptr;
} }
@ -60,14 +58,14 @@ void* named_array_alloc(NamedArray* array, unsigned int* new_id) {
void* named_array_reserve(NamedArray* array, unsigned int id) { void* named_array_reserve(NamedArray* array, unsigned int id) {
if(!named_array_used(array, id)) { if(!named_array_used(array, id)) {
unsigned int j = (id % 8) - 1; unsigned int j = (id % 8);
unsigned int i = id / 8; unsigned int i = id / 8;
assert(!named_array_used(array, id)); assert(!named_array_used(array, id));
array->used_markers[i] |= (unsigned char) 1 << j; array->used_markers[i] |= (unsigned char) 1 << j;
assert(named_array_used(array, id)); assert(named_array_used(array, id));
unsigned char* ptr = &array->elements[(id - 1) * array->element_size]; unsigned char* ptr = &array->elements[id * array->element_size];
memset(ptr, 0, array->element_size); memset(ptr, 0, array->element_size);
return ptr; return ptr;
} }
@ -76,8 +74,6 @@ void* named_array_reserve(NamedArray* array, unsigned int id) {
} }
void named_array_release(NamedArray* array, unsigned int new_id) { void named_array_release(NamedArray* array, unsigned int new_id) {
new_id--;
unsigned int i = new_id / 8; unsigned int i = new_id / 8;
unsigned int j = new_id % 8; unsigned int j = new_id % 8;
@ -93,7 +89,7 @@ void* named_array_get(NamedArray* array, unsigned int id) {
return NULL; return NULL;
} }
return &array->elements[(id - 1) * array->element_size]; return &array->elements[id * array->element_size];
} }
void named_array_cleanup(NamedArray* array) { void named_array_cleanup(NamedArray* array) {