/************************************************************************* * * (c) Copyright 1992,1993,1994 by G & A Technical Software, Inc., * 28 Research Drive, Hampton, Virginia, 23666. * * All Rights Reserved. No part of this software or publication may be * reproduced, stored in a retrieval system, or transmitted, in any form * or by any means, electronic, mechanical, photocopying, recording, or * otherwise without the prior written permission of G & A Technical * Software, Inc. * ************************************************************************* * * Filename: * * Purpose: * * Author: John Burton * * Date: 5/31/94 * ************************************************************************* * * Modification History: * * $Log: s3_memory.c,v $ * Revision 2.1 1994/07/08 18:07:40 jcburt * No changes since last commit. Just bringing everything up to the same * revision number * * Revision 2.0.0.1 1994/06/28 18:48:27 jcburt * S3 Version 2.0 Initial Source * * $Log: s3_memory.c,v $ * Revision 2.1 1994/07/08 18:07:40 jcburt * No changes since last commit. Just bringing everything up to the same * revision number * * Revision 2.0.0.1 1994/06/28 18:48:27 jcburt * S3 Version 2.0 Initial Source * * Revision 1.2 1992/12/01 18:08:39 jcburt * added rcsid character string * ************************************************************************* * * Revision Control Information: * *************************************************************************/ static char rcsid[] = "$Id: s3_memory.c,v 2.1 1994/07/08 18:07:40 jcburt Exp $"; static char rcsrev[] = "$Revision: 2.1 $"; /************************************************************************* * * Include Files * *************************************************************************/ #include "s3_defines.h" #include "s3_typedefs.h" #include "s3_externals.h" #include "s3_globals.h" /************************************************************************* * * Defines and Macros * *************************************************************************/ #define GRAIN 16 #define NBLOCKS 4096 #define NPAGES 4096 #define NREALLOC 256 #define NULLPTR ((MEMPTR)0) #define FREE 0 #define INUSE 0xFFFFFFFF typedef char *MEMPTR; typedef union { double aligned; char bytes[GRAIN]; } MEMBLK; typedef struct { MEMPTR start; size_t size; int status; } MEMINFO; static struct { MEMPTR start; MEMPTR next; size_t free; } page; /************************************************************************* * * Global Variables * *************************************************************************/ static size_t ablocks = 0; static size_t kbytes = 0; static int lastptr = 0; static int lastfree = 0; static int lastpage = 0; static short freelist[MAXPTRS]; static MEMINFO dbuf[MAXPTRS]; static MEMPTR pagelist[NPAGES]; /************************************************************************* * * Module: * * Description: * * Syntax: * * Inputs: * * Outputs: * * Returns: * * Modules Called: * *************************************************************************/ void init_mem() { register int i; pagelist[0] = (MEMPTR)pagelist; pagelist[1] = (MEMPTR)dbuf; for(i=0;i NBLOCKS) { if((area.start = (MEMPTR)calloc(1,nbytes)) == NULL) s3_internal_error(__FILE__,__LINE__,SYS,"getmem: malloc error for %d bytes",nbytes); area.size = nbytes; area.status = INUSE; return area; } if(size > page.free) { if((page.start = (MEMPTR)calloc(1,NBLOCKS * GRAIN)) == NULL) s3_internal_error(__FILE__,__LINE__,SYS,"getmem: malloc error for %d bytes",nbytes); page.next = page.start; page.free = NBLOCKS; /* memset(page.start,0,(NBLOCKS*GRAIN)); */ } area.start = page.next; area.size = nbytes; area.status = INUSE; page.next += nbytes; page.free -= nblocks; return area; } /************************************************************************* * * Module: * * Description: * * Syntax: * * Inputs: * * Outputs: * * Returns: * * Modules Called: * *************************************************************************/ MEMPTR memalloc(char *file,int line, size_t size) { register int found, i, j, l; MEMPTR ptr; i = 0; found = FALSE; while((i= size)); i++; } if(found) { dbuf[j].status = INUSE; ptr = dbuf[j].start; for (l=i;lNDIMS;i++) size *= ds->dimlist[i]->co.value; size = (size % 8 == 0) ? size : (((size/8) + 1) * 8); block = size + 4 * sizeof(int); /* r = getmem(block); */ r = xmalloc(block); ds->start_check = (int *)r; ds->d.data = (byte *)(r + 2*sizeof(int)); ds->stop_check = (int *)(r + 2*sizeof(int) + size); *(ds->start_check) = START; *(ds->stop_check) = STOP; return ds; } /************************************************************************* * * Module: * * Description: * * Syntax: * * Inputs: * * Outputs: * * Returns: * * Modules Called: * *************************************************************************/ char *ymalloc(char *file, int line, unsigned int nbytes) { char *pheap; pheap = (char *) memalloc(file,line,nbytes); return pheap; } /************************************************************************* * * Module: * * Description: * * Syntax: * * Inputs: * * Outputs: * * Returns: * * Modules Called: * *************************************************************************/ char *ycalloc(char *file, int line, unsigned int nelem, unsigned int size) { char *pheap; xcalloc_count++; pheap = (char *) memalloc(file,line,nelem*size); pheap = memset(pheap,0,nelem*size); return pheap; } /************************************************************************* * * Module: * * Description: * * Syntax: * * Inputs: * * Outputs: * * Returns: * * Modules Called: * *************************************************************************/ char *yrealloc(char *file,int line,char *oldp,size_t nbytes) { char *newp; register char **pbuf; register int i; size_t oldsize; int found = FALSE; int okay; if(oldp == NULL) { if(nbytes < NREALLOC) newp = (char *)memalloc(file,line,NREALLOC); else newp = (char *)memalloc(file,line,nbytes); return newp; } else { if(nbytes < NREALLOC) return oldp; else for(i=0;iNDIMS; s3_dprint("in s3_allocate_dataset - ND = %d",ND); s3_dprint("in s3_allocate_dataset - type = %x",ds->type); switch(ds->type) { case BYTE : size = sizeof(byte); break; case CHAR : size = sizeof(char); break; case STRING : size = sizeof(char); ds->dimlist[0]->co.value += 1; break; case INT : size = sizeof(int); break; case REAL : size = sizeof(float); break; case DREAL: size = sizeof(double); break; case COMP: size = sizeof(complex); break; case DCOMP: size = sizeof(dbl_complex); break; } /* if(ND == 0) ds->d.start = (byte *)malloc(size); else */ ds = allocate_memory(ds,size); s3_dprint("allocate_dataset: %s: ds->start_check = %x\n",ds->name,ds->start_check[0]); s3_dprint("allocate_dataset: %s: ds->stop_check = %x\n",ds->name,ds->stop_check[0]); buffer = ds->d.data; nbytes = size; for(i=0;idimlist[i]->co.value; s3_dprint("in allocate_dataset - # bytes allocated = %d",nbytes); /* if(ds->type == CHAR) memset(buffer,(int)blankch,nbytes); else memset(buffer,(int)NULL,nbytes); */ return ds; }