/[ascend]/trunk/ascend/general/mem.c
ViewVC logotype

Diff of /trunk/ascend/general/mem.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2526 by jpye, Thu Jan 19 23:49:44 2012 UTC revision 2527 by jpye, Fri Jan 20 00:44:37 2012 UTC
# Line 102  int    mem_get_byte(long from) Line 102  int    mem_get_byte(long from)
102     return( ((int)c) & mask_I_L(BYTESIZE) );     return( ((int)c) & mask_I_L(BYTESIZE) );
103  }  }
104  #endif /*  0  */  #endif /*  0  */
105  unsigned char mem_get_byte(asc_intptr_t from)  unsigned char mem_get_byte(POINTER from)
106  {  {
107     unsigned char c;     unsigned char c;
108     mv_get(&c, from, 1);     mv_get(&c, from, 1);
109     return(c);     return(c);
110  }  }
111    
112  int mem_get_int(asc_intptr_t from)  int mem_get_int(POINTER from){
 {  
113     int  i;     int  i;
114     mv_get(&i,from,sizeof(int));     mv_get(&i,from,sizeof(int));
115     return(i);     return(i);
116  }  }
117    
118  long    mem_get_long(asc_intptr_t from)  long mem_get_long(POINTER from){
 {  
119     long l;     long l;
120     mv_get(&l,from,sizeof(long));     mv_get(&l,from,sizeof(long));
121     return(l);     return(l);
122  }  }
123    
124  double  mem_get_float(asc_intptr_t from)  double  mem_get_float(POINTER from){
 {  
125     float    f;     float    f;
126     mv_get(&f,from,sizeof(float));     mv_get(&f,from,sizeof(float));
127     return((double)f);     return((double)f);
128  }  }
129    
130  double  mem_get_double(asc_intptr_t from)  double  mem_get_double(POINTER from){
 {  
131     double   d;     double   d;
132     mv_get(&d,from,sizeof(double));     mv_get(&d,from,sizeof(double));
133     return(d);     return(d);
134  }  }
135    
136  void mem_set_byte(asc_intptr_t from, int b)  void mem_set_byte(POINTER from, int b){
 {  
137     char c = (char)b;     char c = (char)b;
138     mv_set(&c,from,1);     mv_set(&c,from,1);
139  }  }
140    
141  void mem_set_int(asc_intptr_t from, int i)  void mem_set_int(POINTER from, int i){
 {  
142     mv_set(&i,from,sizeof(int));     mv_set(&i,from,sizeof(int));
143  }  }
144    
145  void mem_set_long(asc_intptr_t from, long l)  void mem_set_long(POINTER from, long l){
 {  
146     mv_set(&l,from,sizeof(long));     mv_set(&l,from,sizeof(long));
147  }  }
148    
149  void mem_set_float(asc_intptr_t from, double f)  void mem_set_float(POINTER from, double f){
 {  
150     float    ff = (float)f;     float    ff = (float)f;
151     mv_set(&ff,from,sizeof(float));     mv_set(&ff,from,sizeof(float));
152  }  }
153    
154  void mem_set_double(asc_intptr_t from, double d)  void mem_set_double(POINTER from, double d){
 {  
155     mv_set(&d,from,sizeof(double));     mv_set(&d,from,sizeof(double));
156  }  }
157    
158    
159  /*********************** mem_store code. BAA 5/16/95 ***********************/  /*********************** mem_store code. BAA 5/16/95 ***********************/
160    
161  /* according to K&R2 char <--> byte and size_t is a byte count.  /* according to K&R2 char <--> byte and size_t is a byte count.
162     We are coding with those assumptions. (sizeof(char)==1) */     We are coding with those assumptions. (sizeof(char)==1) */
163    
164  #define OK 345676543  #define OK 345676543
165  #define DESTROYED 765434567  #define DESTROYED 765434567
166  #if mem_DEBUG  #if MEM_DEBUG
167  /* ground LIGHTENING */  /* ground LIGHTENING */
168  #undef mem_LIGHTENING  #undef mem_LIGHTENING
169  #define mem_LIGHTENING FALSE  #define mem_LIGHTENING FALSE
# Line 343  static int expand_store(mem_store_t ms, Line 337  static int expand_store(mem_store_t ms,
337    ms->len = newsize; /* set expanded number of bars filled */    ms->len = newsize; /* set expanded number of bars filled */
338    punt = -1;    punt = -1;
339    for (i = oldsize; i < newsize; i++) {    for (i = oldsize; i < newsize; i++) {
340  #if mem_DEBUG  #if MEM_DEBUG
341      ms->pool[i] = (char *)AMEM_calloc(ms->barsize,1);      ms->pool[i] = (char *)AMEM_calloc(ms->barsize,1);
342  #else  #else
343      ms->pool[i] = (char *)AMEM_malloc(ms->barsize);      ms->pool[i] = (char *)AMEM_malloc(ms->barsize);
# Line 358  static int expand_store(mem_store_t ms, Line 352  static int expand_store(mem_store_t ms,
352      /* incomplete expansion */      /* incomplete expansion */
353      if (punt == oldsize) {      if (punt == oldsize) {
354        /* unable to add elements at all. fail */        /* unable to add elements at all. fail */
355        FPRINTF(stderr,"ERROR: (mem) expand_store:  Insufficient memory.\n");        ERROR_REPORTER_HERE(ASC_PROG_ERR,"Insufficient memory.");
356        ms->len = oldsize;        ms->len = oldsize;
357        return 1;        return 1;
358      } else {      } else {
359        /* contract pool to the actual expansion size */        /* contract pool to the actual expansion size */
360        FPRINTF(stderr,"WARNING: (mem) expand_store: Insufficient memory.\n");        ERROR_REPORTER_HERE(ASC_PROG_ERR,"Insufficient memory. Doing partial expansion.");
       FPRINTF(stderr,"                             Doing partial expansion.\n");  
361        ms->len = punt;        ms->len = punt;
362      }      }
363    }    }
# Line 374  static int expand_store(mem_store_t ms, Line 367  static int expand_store(mem_store_t ms,
367    return 0;    return 0;
368  }  }
369    
370    
371  #if !mem_LIGHTENING  #if !mem_LIGHTENING
372  #if mem_DEBUG  #if MEM_DEBUG
373  /*  /*
374  Returns 1 if pointer is to an elt of the store, 0 otherwise.  Returns 1 if pointer is to an elt of the store, 0 otherwise.
375  The case of pointer into store reserved space, but not an elt is checked.  The case of pointer into store reserved space, but not an elt is checked.
376  */  */
377  static int from_store( mem_store_t ms, void *elt)  static int from_store( mem_store_t ms, void *elt){
 {  
378    char *data;    char *data;
379    char **pool;    char **pool;
380    int i;    int i;
# Line 398  static int from_store( mem_store_t ms, v Line 391  static int from_store( mem_store_t ms, v
391        if ( !((data - (*pool)) % ms->eltsize) ) {        if ( !((data - (*pool)) % ms->eltsize) ) {
392          return 1;          return 1;
393        } else {        } else {
394          FPRINTF(stderr,"ERROR: (mem.c) from_store:  Misaligned element\n");          ERROR_REPORTER_HERE(ASC_PROG_ERR,"Misaligned element pointer detected.");
         FPRINTF(stderr,"                            pointer detected.\n");  
395          return 0;          return 0;
396        }        }
397      }      }
# Line 410  static int from_store( mem_store_t ms, v Line 402  static int from_store( mem_store_t ms, v
402  #endif  #endif
403  #endif  #endif
404    
405  void mem_get_stats(struct mem_statistics *mss,  mem_store_t m)  
406  {  void mem_get_stats(struct mem_statistics *mss,  mem_store_t m){
407    if (ISNULL(mss)) {    if (ISNULL(mss)) {
408      FPRINTF(stderr,"ERROR: (mem_get_stats)   Called with NULL struct\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Called with NULL struct mem_statistics pointer.");
     FPRINTF(stderr,"                         mem_statistics.\n");  
409      return;      return;
410    }    }
411    if (check_mem_store(m)>1 ) {    if (check_mem_store(m)>1 ) {
412      ascbzero((void *)mss,(size_t)sizeof(struct mem_statistics));      ascbzero((void *)mss,(size_t)sizeof(struct mem_statistics));
413      FPRINTF(stderr,"ERROR: (mem_get_stats)   Bad mem_store_t given.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Bad mem_store_t given. Returning 0s.");
     FPRINTF(stderr,"                         Returning 0s.\n");  
414      return;      return;
415    }    }
416  #if !mem_LIGHTENING  #if !mem_LIGHTENING
# Line 443  void mem_get_stats(struct mem_statistics Line 433  void mem_get_stats(struct mem_statistics
433    mss->str_wid = m->wid;    mss->str_wid = m->wid;
434  }  }
435    
436    
437  mem_store_t mem_create_store(int length, int width,  mem_store_t mem_create_store(int length, int width,
438                               size_t eltsize, int deltalen, int deltapool)          size_t eltsize, int deltalen, int deltapool
439  {  ){
440    int i, punt;    int i, punt;
441    mem_store_t newms=NULL;    mem_store_t newms=NULL;
442    size_t uelt;    size_t uelt;
443    
444    if (length < 1 || width < 1 || deltalen < 1 ) {    if (length < 1 || width < 1 || deltalen < 1 ) {
445      FPRINTF(stderr,"ERROR: (mem_create_store) : Bad input detected.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Bad input detected.");
446      return NULL;      return NULL;
447    }    }
448    
# Line 463  mem_store_t mem_create_store(int length, Line 454  mem_store_t mem_create_store(int length,
454    
455    uelt = eltsize;    uelt = eltsize;
456    /* check for elt padding needed */    /* check for elt padding needed */
457    if (eltsize % sizeof(void *)) {    if(eltsize % sizeof(void *)) {
458      size_t ptrperelt;      size_t ptrperelt;
459      ptrperelt = eltsize/sizeof(void *) + 1;      ptrperelt = eltsize/sizeof(void *) + 1;
460  #if mem_DEBUG  #if MEM_DEBUG
461      FPRINTF(stderr,"(mem_create_store) Elts of size %d padded to %d\n",      CONSOLE_DEBUG("Elts of size %d padded to %d\n",(unsigned)eltsize,(unsigned)(eltsize = (unsigned)ptrperelt*sizeof(void *)));
       eltsize,(eltsize=ptrperelt*sizeof(void *)));  
462  #else  #else
463      eltsize = ptrperelt*sizeof(void *);      eltsize = ptrperelt*sizeof(void *);
464  #endif  #endif
# Line 477  mem_store_t mem_create_store(int length, Line 467  mem_store_t mem_create_store(int length,
467    /* it could still be user data misalignable, of course, if pointer    /* it could still be user data misalignable, of course, if pointer
468       is not the most restrictive data type for the machine */       is not the most restrictive data type for the machine */
469    
   
470    newms = (mem_store_t)AMEM_calloc(1,sizeof(struct mem_store_header));    newms = (mem_store_t)AMEM_calloc(1,sizeof(struct mem_store_header));
471    if (ISNULL(newms)) {    if (ISNULL(newms)) {
472      FPRINTF(stderr,"ERROR: (mem_create_store) : Insufficient memory.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Insufficient memory.");
473      return NULL;      return NULL;
474    }    }
475    /* the following are all initially 0/NULL by calloc, and should be:    /* the following are all initially 0/NULL by calloc, and should be:
# Line 510  mem_store_t mem_create_store(int length, Line 499  mem_store_t mem_create_store(int length,
499    /* get pool */    /* get pool */
500    newms->pool = (char **)AMEM_calloc((size_t)length,sizeof(char *));    newms->pool = (char **)AMEM_calloc((size_t)length,sizeof(char *));
501    if (ISNULL(newms->pool)) {    if (ISNULL(newms->pool)) {
502      FPRINTF(stderr,"ERROR: (mem_create_store) : Insufficient memory.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Insufficient memory.");
503      newms->integrity = DESTROYED;      newms->integrity = DESTROYED;
504      AMEM_free(newms);      AMEM_free(newms);
505      return NULL;      return NULL;
# Line 528  mem_store_t mem_create_store(int length, Line 517  mem_store_t mem_create_store(int length,
517    
518    /* drain it if can't fill it */    /* drain it if can't fill it */
519   if (punt != -1) {   if (punt != -1) {
520      FPRINTF(stderr,"ERROR: (mem_create_store) : Insufficient memory.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Insufficient memory.");
521      for (i = 0; i < punt; i++) {      for (i = 0; i < punt; i++) {
522        AMEM_free(newms->pool[i]);        AMEM_free(newms->pool[i]);
523      }      }
# Line 540  mem_store_t mem_create_store(int length, Line 529  mem_store_t mem_create_store(int length,
529    return newms;    return newms;
530  }  }
531    
532  void *mem_get_element(mem_store_t ms)  
533  {  void *mem_get_element(mem_store_t ms){
534    /* no automatic variables please */    /* no automatic variables please */
535    register struct mem_element *elt;    register struct mem_element *elt;
536    /* in a test on the alpha, though, making elt static global slowed it */    /* in a test on the alpha, though, making elt static global slowed it */
537    
538    if (ISNULL(ms)) {    if (ISNULL(ms)) {
539      FPRINTF(stderr,"ERROR: (mem_get_element)  Called with NULL store.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Called with NULL store.");
540      return NULL;      return NULL;
541    }    }
542    /* recycling */    /* recycling */
# Line 572  void *mem_get_element(mem_store_t ms) Line 561  void *mem_get_element(mem_store_t ms)
561    if (ms->curbar == ms->len) {    if (ms->curbar == ms->len) {
562      /* attempt to expand pool if all allocated */      /* attempt to expand pool if all allocated */
563      if ( expand_store(ms,1) ) {      if ( expand_store(ms,1) ) {
564        FPRINTF(stderr,"ERROR: (mem_get_element)  Insufficient memory.\n");        ERROR_REPORTER_HERE(ASC_PROG_ERR,"Insufficient memory.");
565        return NULL;        return NULL;
566      }      }
567    }    }
# Line 590  void *mem_get_element(mem_store_t ms) Line 579  void *mem_get_element(mem_store_t ms)
579    return (void *)elt;    return (void *)elt;
580  }  }
581    
582  void mem_get_element_list(mem_store_t ms, int nelts, void **ary)  
583  {  void mem_get_element_list(mem_store_t ms, int nelts, void **ary){
584    FPRINTF(stderr,"ERROR: mem_get_element_list NOT implemented\n");    ERROR_REPORTER_HERE(ASC_PROG_ERR,"mem_get_element_list NOT implemented");
585    if (ISNULL(ms) || ISNULL(ary)) {    if (ISNULL(ms) || ISNULL(ary)) {
586      FPRINTF(stderr,"ERROR:   mem_get_element_list   Called with NULL\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Called with NULL array or mem_store_t");
     FPRINTF(stderr,"                                array or mem_store_t");  
587      return;      return;
588    }    }
589    if (nelts <1) {    if (nelts <1) {
590      FPRINTF(stderr,"WARNING:  mem_get_element_list   Called with request\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Called with request for 0 elements.");
     FPRINTF(stderr,"                                 for 0 elements.");  
591      return;      return;
592    }    }
593    ary[0]=NULL;    ary[0]=NULL;
594  }  }
595    
596  void mem_free_element(mem_store_t ms, void *ptr)  
597  {  void mem_free_element(mem_store_t ms, void *ptr){
598    register struct mem_element *elt;    register struct mem_element *elt;
599    
600    if (ISNULL(ptr)) return;    if (ISNULL(ptr)) return;
601    elt = (struct mem_element *)ptr;    elt = (struct mem_element *)ptr;
602    
603  #if !mem_LIGHTENING  #if !mem_LIGHTENING
604  #if mem_DEBUG  #if MEM_DEBUG
605    if (check_mem_store(ms)) {    if (check_mem_store(ms)) {
606      FPRINTF(stderr,"ERROR: (mem_free_element)  Fishy mem_store_t.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Fishy mem_store_t. Element not recycled.");
     FPRINTF(stderr,"                           Element not recycled.\n");  
607      return;      return;
608      /* at this point we have no way to get back at the abandoned element */      /* at this point we have no way to get back at the abandoned element */
609    }    }
610    /* check for belongs to this mem_store_t */    /* check for belongs to this mem_store_t */
611    if (!from_store(ms,ptr)) {    if (!from_store(ms,ptr)) {
612      FPRINTF(stderr,"ERROR: (mem_free_element)  Spurious element detected.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Spurious element detected. Element ignored.");
     FPRINTF(stderr,"                           Element ignored.\n");  
613      return;      return;
614    }    }
615  #endif  #endif
# Line 641  void mem_free_element(mem_store_t ms, vo Line 626  void mem_free_element(mem_store_t ms, vo
626    ms->retned++;    ms->retned++;
627    ms->inuse--;    ms->inuse--;
628    if (ms->inuse < 0) {    if (ms->inuse < 0) {
629      FPRINTF(stderr,"ERROR: (mem_free_element) More elements freed than\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"More elements freed than have been handed out. (%d)",abs(ms->inuse));
     FPRINTF(stderr,"                          have been handed out. (%d)\n",  
       abs(ms->inuse));  
630    }    }
631  #endif  #endif
632    return;    return;
633  }  }
634    
635  void mem_clear_store(mem_store_t ms) {  
636    void mem_clear_store(mem_store_t ms){
637    if ( check_mem_store(ms) > 1 ) {    if ( check_mem_store(ms) > 1 ) {
638      FPRINTF(stderr,"ERROR: (mem_clear_store)  Bad mem_store_t given.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Bad mem_store_t given. Not cleared.");
     FPRINTF(stderr,"                          Not cleared.\n");  
639      return;      return;
640    }    }
641  #if mem_DEBUG  #if MEM_DEBUG
642    if (ms->inuse || ms->highwater - ms->onlist ) {    if (ms->inuse || ms->highwater - ms->onlist ) {
643      FPRINTF(stderr,"WARNING: (mem_clear_store)  In use elements in given\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"In use elements in given mem_store_t are cleared. Don't refer to them again.");
     FPRINTF(stderr,"                            mem_store_t are cleared.\n");  
     FPRINTF(stderr,"                            Don't refer to them again.\n");  
644    }    }
645  #endif  #endif
646    ms->retned += ms->inuse;    ms->retned += ms->inuse;
647    if (ms->active - ms->retned ||    if (ms->active - ms->retned ||
648        ms->onlist + ms->inuse - ms->highwater ||        ms->onlist + ms->inuse - ms->highwater ||
649        ms->curelt + ms->curbar*ms->wid - ms->highwater) {        ms->curelt + ms->curbar*ms->wid - ms->highwater) {
650      FPRINTF(stderr,"Warning: mem_clear_store: Element imbalance detected.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Element imbalance detected.");
651    }    }
652    ms->inuse = 0;    ms->inuse = 0;
653    ms->curbar = 0;    ms->curbar = 0;
# Line 676  void mem_clear_store(mem_store_t ms) { Line 657  void mem_clear_store(mem_store_t ms) {
657    ms->list = NULL;    ms->list = NULL;
658  }  }
659    
660  void mem_destroy_store(mem_store_t ms)  
661  {  void mem_destroy_store(mem_store_t ms){
662    int i;    int i;
663  #if mem_DEBUG  #if MEM_DEBUG
664    if ( (i=check_mem_store(ms))==2 ) {    if ( (i=check_mem_store(ms))==2 ) {
665      FPRINTF(stderr,"ERROR: (mem_destroy_store)  Bad mem_store_t given.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Bad mem_store_t given. Not destroyed.");
     FPRINTF(stderr,"                            Not destroyed.\n");  
666      return;      return;
667    }    }
668    if ( i ) {    if ( i ) {
669      FPRINTF(stderr,      ERROR_REPORTER_HERE(ASC_PROG_WARNING,"Suspicious mem_store_t given. Destroyed anyway.");
       "WARNING: (mem_destroy_store)  Suspicious mem_store_t given.\n");  
     FPRINTF(stderr,"                            Destroyed anyway.\n");  
670      return;      return;
671    }    }
672    if (ms->inuse || ms->highwater - ms->onlist ) {    if (ms->inuse || ms->highwater - ms->onlist ) {
673      FPRINTF(stderr,"WARNING: (mem_destroy_store) In use elements in given\n");      ERROR_REPORTER_HERE(ASC_PROG_WARNING,"In use elements in given mem_store_t are cleared. Don't refer to them again.");
     FPRINTF(stderr,"                             mem_store_t are cleared.\n");  
     FPRINTF(stderr,"                             Don't refer to them again.\n");  
674    }    }
675  #else  #else
676    if (ISNULL(ms)  || ms->integrity != OK) {    if (ISNULL(ms)  || ms->integrity != OK) {
677      FPRINTF(stderr,"ERROR: (mem_destroy_store)  Bad mem_store_t given.\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Bad mem_store_t given. Not destroyed.");
     FPRINTF(stderr,"                            Not destroyed.\n");  
678      return;      return;
679    }    }
680  #endif  #endif
# Line 712  void mem_destroy_store(mem_store_t ms) Line 687  void mem_destroy_store(mem_store_t ms)
687    return;    return;
688  }  }
689    
690  void mem_print_store(FILE *fp, mem_store_t ms, unsigned detail)  
691  {  void mem_print_store(FILE *fp, mem_store_t ms, unsigned detail){
692    if (ISNULL(fp) || ISNULL(ms)) {    if (ISNULL(fp) || ISNULL(ms)) {
693      FPRINTF(stderr,"ERROR: (mem_print_store) Called with NULL\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Called with NULL FILE or mem_store_t");
     FPRINTF(stderr,"                         FILE or mem_store_t\n");  
694      return;      return;
695    }    }
696    if (check_mem_store(ms)>1) {    if (check_mem_store(ms)>1) {
697      FPRINTF(stderr,"ERROR: (mem_print_store) Called with bad mem_store_t\n");      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Called with bad mem_store_t");
698      return;      return;
699    }    }
700    FPRINTF(fp,"mem_store_t statistics:\n");    FPRINTF(fp,"mem_store_t statistics:\n");
# Line 780  void mem_print_store(FILE *fp, mem_store Line 754  void mem_print_store(FILE *fp, mem_store
754    return;    return;
755  }  }
756    
757  size_t mem_sizeof_store(mem_store_t ms)  
758  {  size_t mem_sizeof_store(mem_store_t ms){
759    register size_t siz;    register size_t siz;
760    if (check_mem_store(ms)>1) return (size_t)0;    if (check_mem_store(ms)>1) return (size_t)0;
761    siz = sizeof(struct mem_store_header);    /* header */    siz = sizeof(struct mem_store_header);    /* header */

Legend:
Removed from v.2526  
changed lines
  Added in v.2527

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22