#include <stdio.h>
#include <stdlib.h>
#include "dynP.h"
int _DynResize(obj, req)
DynObjectP obj;
int req;
{
int cnt, size;
if (obj->size > req)
return DYN_OK;
else if (obj->inc > 0)
return _DynRealloc(obj, (req - obj->size) / obj->inc + 1);
else {
if (obj->size == 0)
size = -obj->inc;
else
size = obj->size;
while (size <= req)
size <<= 1;
return _DynRealloc(obj, size);
}
}
int _DynRealloc(obj, num_incs)
DynObjectP obj;
int num_incs;
{
DynPtr temp;
int new_size_in_bytes;
if (obj->inc > 0)
new_size_in_bytes = obj->el_size*(obj->size + obj->inc*num_incs);
else
new_size_in_bytes = obj->el_size*num_incs;
if (obj->debug)
fprintf(stderr,
"dyn: alloc: Increasing object by %d bytes (%d incs).\n",
new_size_in_bytes - obj->el_size*obj->size,
num_incs);
temp = (DynPtr) realloc(obj->array, new_size_in_bytes);
if (temp == NULL) {
if (obj->debug)
fprintf(stderr, "dyn: alloc: Out of memory.\n");
return DYN_NOMEM;
}
else {
obj->array = temp;
if (obj->inc > 0)
obj->size += obj->inc*num_incs;
else
obj->size = num_incs;
}
if (obj->debug)
fprintf(stderr, "dyn: alloc: done.\n");
return DYN_OK;
}