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 { |
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); |
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 |
*/ |
*/ |
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 |
|
|
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 |
|
|
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 |
/* ************************************************************************* */ |
/* ************************************************************************* */ |