simplified error checking, out of memory now results in an immediate crash when trying to expand world
parent
5e001bc345
commit
59de57fe3a
|
@ -1,5 +1,6 @@
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
#include "assert.h"
|
||||||
#include "math/vec.h"
|
#include "math/vec.h"
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
|
||||||
|
@ -18,18 +19,19 @@ int _expand_world() {
|
||||||
new_num = 16;
|
new_num = 16;
|
||||||
}
|
}
|
||||||
object_t** new_list = realloc(_world_objects.objects, new_num * sizeof(object_t*));
|
object_t** new_list = realloc(_world_objects.objects, new_num * sizeof(object_t*));
|
||||||
if(new_list == NULL) {
|
|
||||||
printf("ERROR: failed to expand world when requesting additional objects\n");
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
for(size_t i = _world_objects.num; i < new_num; ++i) {
|
|
||||||
new_list[i] = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
_world_objects.objects = new_list;
|
if(new_list == NULL) {
|
||||||
_world_objects.num = new_num;
|
assert(!"ERROR: Out of memory");
|
||||||
return 1;
|
exit(-10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(size_t i = _world_objects.num; i < new_num; ++i) {
|
||||||
|
new_list[i] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
_world_objects.objects = new_list;
|
||||||
|
_world_objects.num = new_num;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
|
@ -40,11 +42,8 @@ size_t _find_free_object() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t num = _world_objects.num;
|
size_t num = _world_objects.num;
|
||||||
if(_expand_world()) {
|
_expand_world();
|
||||||
return num;
|
return num;
|
||||||
} else {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void world_clear() {
|
void world_clear() {
|
||||||
|
|
Loading…
Reference in New Issue