/[radius]/radius/lib/mem.c
ViewVC logotype

Diff of /radius/lib/mem.c

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

revision 1.7 by gray, Fri Aug 2 11:16:34 2002 UTC revision 1.8 by gray, Fri Apr 18 05:27:09 2003 UTC
# Line 29  static char rcsid[] = Line 29  static char rcsid[] =
29  #include <stdio.h>  #include <stdio.h>
30  #include <sysdep.h>  #include <sysdep.h>
31  #include <radius.h>  #include <radius.h>
 #include <pthread.h>  
32    
33  typedef struct entry *Entry;  typedef struct entry *Entry;
34  struct entry {  struct entry {
# Line 59  union bucket { Line 58  union bucket {
58  };  };
59    
60  Bucketclass bucket_class;  Bucketclass bucket_class;
61  static pthread_mutex_t mem_mutex = PTHREAD_MUTEX_INITIALIZER;  LOCK_DECLARE(mem_lock)
62    
63  #define mem_lock()    pthread_mutex_lock(&mem_mutex)  #define mem_lock()    LOCK_SET(mem_lock)
64  #define mem_unlock()  pthread_mutex_unlock(&mem_mutex)  #define mem_unlock()  LOCK_RELEASE(mem_lock)
65    
66  void *alloc_page();  void *alloc_page();
67  static Bucketclass alloc_class(size_t size);  static Bucketclass alloc_class(size_t size);
# Line 488  typedef union { Line 487  typedef union {
487    
488  #define BLKCNT(length) ((length + HDRSIZE + MINSTRSIZE - 1) / (MINSTRSIZE - 1))  #define BLKCNT(length) ((length + HDRSIZE + MINSTRSIZE - 1) / (MINSTRSIZE - 1))
489    
490  static pthread_mutex_t string_mutex = PTHREAD_MUTEX_INITIALIZER;  LOCK_DECLARE(string_lock)
491    
492  /* string_alloc(): Allocate a string of given length  /* string_alloc(): Allocate a string of given length
493   */   */
# Line 534  string_dup(str) Line 533  string_dup(str)
533    
534          if (!str)          if (!str)
535                  return NULL;                  return NULL;
536          pthread_mutex_lock(&string_mutex);          LOCK_SET(string_lock);
537          hp = (STRHDR*)str - 1;          hp = (STRHDR*)str - 1;
538          if (hp->s.nref == 255)  /* FIXME: use limits.h */          if (hp->s.nref == 255)  /* FIXME: use limits.h */
539                  str = string_create(str);                  str = string_create(str);
540          else          else
541                  hp->s.nref++;                  hp->s.nref++;
542          pthread_mutex_unlock(&string_mutex);          LOCK_RELEASE(string_lock);
543          return str;          return str;
544  }  }
545    
# Line 558  string_replace(str, string_value) Line 557  string_replace(str, string_value)
557          if (!*str)          if (!*str)
558                  return *str = string_create(string_value);                  return *str = string_create(string_value);
559                    
560          pthread_mutex_lock(&string_mutex);          LOCK_SET(string_lock);
561          hp = (STRHDR*)*str - 1;          hp = (STRHDR*)*str - 1;
562          if ( hp->s.nref > 1 || hp->s.nblk < BLKCNT(length) ) {          if ( hp->s.nref > 1 || hp->s.nblk < BLKCNT(length) ) {
563                  pthread_mutex_unlock(&string_mutex);                  LOCK_RELEASE(string_lock);
564                  string_free(*str);                  string_free(*str);
565                  pthread_mutex_lock(&string_mutex);                  LOCK_SET(string_lock);
566                  *str = string_alloc(length + 1);                  *str = string_alloc(length + 1);
567          }          }
568          strcpy(*str, string_value);          strcpy(*str, string_value);
569          pthread_mutex_unlock(&string_mutex);          LOCK_RELEASE(string_lock);
570          return *str;          return *str;
571  }  }
572    
# Line 583  string_free(str) Line 582  string_free(str)
582          if (!str)          if (!str)
583                  return;                  return;
584                    
585          pthread_mutex_lock(&string_mutex);          LOCK_SET(string_lock);
586          hp = (STRHDR*)str - 1;          hp = (STRHDR*)str - 1;
587          if (--hp->s.nref == 0)          if (--hp->s.nref == 0)
588                  mem_cfree(hp, hp->s.nblk);                  mem_cfree(hp, hp->s.nblk);
589          pthread_mutex_unlock(&string_mutex);          LOCK_RELEASE(string_lock);
590  }  }
591    
592  /* ************************************************************************* */  /* ************************************************************************* */

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26