17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * This file is part of libdyn.a, the C Dynamic Object library. It
37c478bd9Sstevel@tonic-gate * contains the source code for the internal function _DynRealloc().
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * There are no restrictions on this code; however, if you make any
67c478bd9Sstevel@tonic-gate * changes, I request that you document them so that I do not get
77c478bd9Sstevel@tonic-gate * credit or blame for your modifications.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
107c478bd9Sstevel@tonic-gate * and MIT-Project Athena, 1989.
117c478bd9Sstevel@tonic-gate */
127c478bd9Sstevel@tonic-gate
137c478bd9Sstevel@tonic-gate #include <stdio.h>
147c478bd9Sstevel@tonic-gate #include <stdlib.h>
157c478bd9Sstevel@tonic-gate
167c478bd9Sstevel@tonic-gate #include "dynP.h"
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate * Resize the array so that element req exists.
207c478bd9Sstevel@tonic-gate */
_DynResize(obj,req)217c478bd9Sstevel@tonic-gate int _DynResize(obj, req)
227c478bd9Sstevel@tonic-gate DynObjectP obj;
237c478bd9Sstevel@tonic-gate int req;
247c478bd9Sstevel@tonic-gate {
257c478bd9Sstevel@tonic-gate int cnt, size;
26*1da57d55SToomas Soome
277c478bd9Sstevel@tonic-gate if (obj->size > req)
287c478bd9Sstevel@tonic-gate return DYN_OK;
297c478bd9Sstevel@tonic-gate else if (obj->inc > 0)
307c478bd9Sstevel@tonic-gate return _DynRealloc(obj, (req - obj->size) / obj->inc + 1);
317c478bd9Sstevel@tonic-gate else {
327c478bd9Sstevel@tonic-gate if (obj->size == 0)
337c478bd9Sstevel@tonic-gate size = -obj->inc;
34*1da57d55SToomas Soome else
357c478bd9Sstevel@tonic-gate size = obj->size;
36*1da57d55SToomas Soome
377c478bd9Sstevel@tonic-gate while (size <= req)
387c478bd9Sstevel@tonic-gate size <<= 1;
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate return _DynRealloc(obj, size);
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate }
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate * Resize the array by num_incs units. If obj->inc is positive, this
467c478bd9Sstevel@tonic-gate * means make it obj->inc*num_incs elements larger. If obj->inc is
477c478bd9Sstevel@tonic-gate * negative, this means make the array num_incs elements long.
48*1da57d55SToomas Soome *
497c478bd9Sstevel@tonic-gate * Ideally, this function should not be called from outside the
507c478bd9Sstevel@tonic-gate * library. However, nothing will break if it is.
517c478bd9Sstevel@tonic-gate */
_DynRealloc(obj,num_incs)527c478bd9Sstevel@tonic-gate int _DynRealloc(obj, num_incs)
537c478bd9Sstevel@tonic-gate DynObjectP obj;
547c478bd9Sstevel@tonic-gate int num_incs;
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate DynPtr temp;
577c478bd9Sstevel@tonic-gate int new_size_in_bytes;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate if (obj->inc > 0)
607c478bd9Sstevel@tonic-gate new_size_in_bytes = obj->el_size*(obj->size + obj->inc*num_incs);
617c478bd9Sstevel@tonic-gate else
627c478bd9Sstevel@tonic-gate new_size_in_bytes = obj->el_size*num_incs;
63*1da57d55SToomas Soome
647c478bd9Sstevel@tonic-gate if (obj->debug)
657c478bd9Sstevel@tonic-gate fprintf(stderr,
667c478bd9Sstevel@tonic-gate "dyn: alloc: Increasing object by %d bytes (%d incs).\n",
677c478bd9Sstevel@tonic-gate new_size_in_bytes - obj->el_size*obj->size,
687c478bd9Sstevel@tonic-gate num_incs);
69*1da57d55SToomas Soome
707c478bd9Sstevel@tonic-gate temp = (DynPtr) realloc(obj->array, new_size_in_bytes);
717c478bd9Sstevel@tonic-gate if (temp == NULL) {
727c478bd9Sstevel@tonic-gate if (obj->debug)
737c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: alloc: Out of memory.\n");
747c478bd9Sstevel@tonic-gate return DYN_NOMEM;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate else {
777c478bd9Sstevel@tonic-gate obj->array = temp;
787c478bd9Sstevel@tonic-gate if (obj->inc > 0)
797c478bd9Sstevel@tonic-gate obj->size += obj->inc*num_incs;
807c478bd9Sstevel@tonic-gate else
817c478bd9Sstevel@tonic-gate obj->size = num_incs;
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate if (obj->debug)
857c478bd9Sstevel@tonic-gate fprintf(stderr, "dyn: alloc: done.\n");
86*1da57d55SToomas Soome
877c478bd9Sstevel@tonic-gate return DYN_OK;
887c478bd9Sstevel@tonic-gate }
89