#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <fm/fmd_api.h>
#include "alloc.h"
#include "out.h"
#include "stats.h"
extern fmd_hdl_t *Hdl;
#define HDRSIZ sizeof (long long)
static struct stats *Malloctotal;
static struct stats *Freetotal;
static struct stats *Malloccount;
static struct stats *Freecount;
static int totalcount;
void
alloc_init(void)
{
Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1);
Freetotal = stats_new_counter("free.total", "bytes freed", 1);
Malloccount = stats_new_counter("alloc.calls", "alloc calls", 1);
Freecount = stats_new_counter("free.calls", "free calls", 1);
}
void
alloc_fini(void)
{
struct stats *mt, *ft, *mc, *fc;
mt = Malloctotal;
ft = Freetotal;
mc = Malloccount;
fc = Freecount;
Malloctotal = NULL;
Freetotal = NULL;
Malloccount = NULL;
Freecount = NULL;
stats_delete(mt);
stats_delete(ft);
stats_delete(mc);
stats_delete(fc);
}
void *
alloc_malloc(size_t nbytes, const char *fname, int line)
{
char *retval;
ASSERT(nbytes > 0);
retval = fmd_hdl_alloc(Hdl, nbytes + HDRSIZ, FMD_SLEEP);
bcopy((void *)&nbytes, (void *)retval, sizeof (nbytes));
retval += HDRSIZ;
if (Malloctotal)
stats_counter_add(Malloctotal, nbytes);
if (Malloccount)
stats_counter_bump(Malloccount);
totalcount += nbytes + HDRSIZ;
return ((void *)retval);
}
void *
alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line)
{
void *retval = alloc_malloc(nbytes, fname, line);
if (ptr != NULL) {
size_t osize;
bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize,
sizeof (osize));
bcopy(ptr, retval, (osize < nbytes) ? osize : nbytes);
alloc_free((char *)ptr, fname, line);
}
return (retval);
}
char *
alloc_strdup(const char *ptr, const char *fname, int line)
{
char *retval = alloc_malloc(strlen(ptr) + 1, fname, line);
(void) strcpy(retval, ptr);
return (retval);
}
void
alloc_free(void *ptr, const char *fname, int line)
{
size_t osize;
ASSERT(ptr != NULL);
bcopy((void *)((char *)ptr - HDRSIZ), (void *)&osize, sizeof (osize));
fmd_hdl_free(Hdl, (char *)ptr - HDRSIZ, osize + HDRSIZ);
if (Freetotal)
stats_counter_add(Freetotal, osize);
if (Freecount)
stats_counter_bump(Freecount);
totalcount -= osize + HDRSIZ;
}
int
alloc_total()
{
return (totalcount);
}
void *
alloc_xmalloc(size_t nbytes)
{
char *retval;
ASSERT(nbytes > 0);
retval = fmd_hdl_alloc(Hdl, nbytes, FMD_SLEEP);
if (Malloctotal)
stats_counter_add(Malloctotal, nbytes);
if (Malloccount)
stats_counter_bump(Malloccount);
totalcount += nbytes;
return ((void *)retval);
}
void
alloc_xfree(void *ptr, size_t size)
{
ASSERT(ptr != NULL);
fmd_hdl_free(Hdl, (char *)ptr, size);
if (Freetotal)
stats_counter_add(Freetotal, size);
if (Freecount)
stats_counter_bump(Freecount);
totalcount -= size;
}