#if (defined(
__GNUC__) && (
__GNUC__ >
2))
#else
#endif
#include "../daemon/netsite.h"
#ifdef XP_WIN32
#include <windows.h>
#include <process.h>
#endif
static int thread_malloc_key = -
1;
static char* temp_dir =
NULL;
#ifdef XP_WIN32
#else
typedef void (newhandler)(
void);
static newhandler *original_newhandler =
0;
#endif
#include "pool.h"
#include "systhr.h"
#define MALLOC_KEY \
((
pool_handle_t *)(thread_malloc_key != -
1 ? systhread_getdata(thread_malloc_key) :
NULL))
#ifdef MCC_DEBUG
#define DEBUG_MALLOC
#endif
#ifdef DEBUG_MALLOC
#define DEBUG_MAGIC 0x12345678
#define DEBUG_MARGIN 32
#define DEBUG_MARGIN_CHAR '*'
#define DEBUG_MALLOC_CHAR '.'
#define DEBUG_FREE_CHAR 'X'
#endif
NSAPI_PUBLIC char *system_version()
{
return "webserver/alpha";
}
NSAPI_PUBLIC pool_handle_t *system_pool(
void)
{
return MALLOC_KEY;
}
NSAPI_PUBLIC void *system_malloc(
int size)
{
void *ret;
ret = pool_malloc(
MALLOC_KEY, size);
if (!ret) {
}
return ret;
}
NSAPI_PUBLIC void *system_calloc(
int size)
{
void *ret;
ret = pool_malloc(
MALLOC_KEY, size);
if(ret) {
ZERO(ret, size);
}
else {
}
return ret;
}
NSAPI_PUBLIC void *system_realloc(
void *ptr,
int size)
{
void *ret;
ret = pool_realloc(
MALLOC_KEY, ptr, size);
if (!ret) {
}
return ret;
}
NSAPI_PUBLIC void system_free(
void *ptr)
{
pool_free(
MALLOC_KEY, ptr);
}
NSAPI_PUBLIC char *system_strdup(
const char *ptr)
{
char *ret;
ret = pool_strdup(
MALLOC_KEY, ptr);
if (!ret) {
}
return ret;
}
NSAPI_PUBLIC void *system_malloc_perm(
int size)
{
void *ret;
#ifndef DEBUG_MALLOC
ret = malloc(size);
#else
char *ptr = (
char *)malloc(size +
2*
DEBUG_MARGIN+
2*
sizeof(
int));
char *real_ptr;
int *magic;
int *length;
magic = (
int *)ptr;
*magic =
DEBUG_MAGIC;
ptr +=
sizeof(
int);
length = (
int *)ptr;
*length = size;
ptr +=
sizeof(
int);
memset(ptr,
DEBUG_MARGIN_CHAR,
DEBUG_MARGIN);
ptr +=
DEBUG_MARGIN;
memset(ptr,
DEBUG_MALLOC_CHAR, size);
real_ptr = ptr;
ptr += size;
memset(ptr,
DEBUG_MARGIN_CHAR,
DEBUG_MARGIN);
ret = real_ptr;
#endif
if (!ret) {
}
return ret;
}
NSAPI_PUBLIC void *system_calloc_perm(
int size)
{
void *ret = system_malloc_perm(size);
if(ret)
ZERO(ret, size);
return ret;
}
NSAPI_PUBLIC void *system_realloc_perm(
void *ptr,
int size)
{
void *ret;
#ifndef DEBUG_MALLOC
ret = realloc(ptr, size);
#else
int *magic, *length;
char *baseptr;
char *cptr;
if (ptr ==
NULL)
return system_malloc_perm(size);
cptr = (
char *)ptr -
DEBUG_MARGIN -
2 *
sizeof(
int);
magic = (
int *)cptr;
if (*magic ==
DEBUG_MAGIC) {
cptr +=
sizeof(
int);
length = (
int *)cptr;
if (*length < size) {
char *newptr = (
char *)system_malloc_perm(size);
memcpy(newptr, ptr, *length);
system_free_perm(ptr);
ret = newptr;
}
else {
ret = ptr;
}
}
else {
ereport(
LOG_WARN, XP_GetAdminString(DBT_systemReallocSmallerSize));
ret = realloc(ptr, size);
}
#endif
if (!ret) {
}
return ret;
}
NSAPI_PUBLIC void system_free_perm(
void *ptr)
{
#ifdef DEBUG_MALLOC
int *length, *magic;
char *baseptr, *cptr;
int index;
NS_ASSERT(ptr);
cptr = baseptr = ((
char *)ptr) -
DEBUG_MARGIN -
2*
sizeof(
int);
magic = (
int *)cptr;
if (*magic ==
DEBUG_MAGIC) {
cptr +=
sizeof(
int);
length = (
int *)cptr;
cptr +=
sizeof(
int);
for (index=
0; index<
DEBUG_MARGIN; index++)
if (cptr[index] !=
DEBUG_MARGIN_CHAR) {
ereport(
LOG_CATASTROPHE, XP_GetAdminString(DBT_systemRFreeCorruptMemoryPre));
break;
}
cptr +=
DEBUG_MARGIN + *length;
for (index=
0; index<
DEBUG_MARGIN; index++)
if (cptr[index] !=
DEBUG_MARGIN_CHAR) {
ereport(
LOG_CATASTROPHE, XP_GetAdminString(DBT_systemRFreeCorruptMemoryPost));
break;
}
memset(baseptr,
DEBUG_FREE_CHAR, *length +
2*
DEBUG_MARGIN+
sizeof(
int));
}
else {
ereport(
LOG_CATASTROPHE, XP_GetAdminString(DBT_systemRFreeUnallocatedMem));
}
free(baseptr);
#else
free(ptr);
#endif
}
NSAPI_PUBLIC char *system_strdup_perm(
const char *ptr)
{
char *ret;
#ifndef DEBUG_MALLOC
ret = strdup(ptr);
#else
int len = strlen(ptr);
char *nptr = (
char *)system_malloc_perm(len+
1);
memcpy(nptr, ptr, len);
nptr[len] =
'\0';
ret = nptr;
#endif
if (!ret) {
}
return ret;
}
NSAPI_PUBLIC void system_set_temp_dir(
const char *dir)
{
temp_dir =
STRDUP(dir);
}
NSAPI_PUBLIC const char *system_get_temp_dir(
void)
{
char* dir = temp_dir;
if (!dir) {
#ifdef XP_WIN32
dir = getenv(
"TEMP");
if (!dir) dir = getenv(
"TMP");
if (!dir) dir =
"C:\\TEMP";
#else
dir =
"/tmp";
#endif
}
return dir;
}
int system_close(
int fd) {
while(close(fd)) {
if(errno !=
EINTR) {
return -
1;
}
else {
log_ereport(
LOG_DEBUG,
"close interrupted by signal");
}
}
return 0;
}
NSAPI_PUBLIC int
getThreadMallocKey(
void)
{
return thread_malloc_key;
}
NSAPI_PUBLIC void
InitThreadMallocKey(
void)
{
}