xrdp/tests/memtest/libmem.c

412 lines
9.9 KiB
C
Raw Normal View History

2013-06-29 06:37:03 +04:00
#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif
2013-06-29 06:37:03 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "log.h"
2013-06-29 09:11:15 +04:00
#define ALIGN_BY 32
2013-06-29 06:37:03 +04:00
#define ALIGN_BY_M1 (ALIGN_BY - 1)
#define ALIGN(_in) (((_in) + ALIGN_BY_M1) & (~ALIGN_BY_M1))
2013-06-29 09:11:15 +04:00
2013-06-29 06:37:03 +04:00
struct mem_item
{
unsigned int addr;
int bytes;
struct mem_item *next;
struct mem_item *prev;
2013-06-29 06:37:03 +04:00
};
struct mem_info
{
unsigned int addr;
int bytes;
int flags;
struct mem_item *free_head;
struct mem_item *free_tail;
struct mem_item *used_head;
struct mem_item *used_tail;
int total_bytes;
2013-06-29 06:37:03 +04:00
};
/*****************************************************************************/
static int
libmem_free_mem_item(struct mem_info *self, struct mem_item *mi)
2013-06-29 06:37:03 +04:00
{
if (self == 0 || mi == 0)
{
return 0;
}
if (mi->prev != 0)
{
mi->prev->next = mi->next;
}
if (mi->next != 0)
{
mi->next->prev = mi->prev;
}
if (mi == self->free_head)
{
self->free_head = mi->next;
}
if (mi == self->free_tail)
{
self->free_tail = mi->prev;
}
if (mi == self->used_head)
{
self->used_head = mi->next;
}
if (mi == self->used_tail)
{
self->used_tail = mi->prev;
}
free(mi);
2013-06-29 06:37:03 +04:00
return 0;
}
/*****************************************************************************/
void *
2013-06-29 06:37:03 +04:00
libmem_init(unsigned int addr, int bytes)
{
struct mem_info *self;
struct mem_item *mi;
2013-06-29 06:37:03 +04:00
self = (struct mem_info *)malloc(sizeof(struct mem_info));
memset(self, 0, sizeof(struct mem_info));
self->addr = addr;
self->bytes = bytes;
//self->flags = 1;
mi = (struct mem_item *)malloc(sizeof(struct mem_item));
memset(mi, 0, sizeof(struct mem_item));
mi->addr = addr;
mi->bytes = bytes;
self->free_head = mi;
self->free_tail = mi;
return self;
2013-06-29 06:37:03 +04:00
}
/*****************************************************************************/
void
libmem_deinit(void *aself)
2013-06-29 06:37:03 +04:00
{
struct mem_info *self;
2013-06-29 06:37:03 +04:00
self = (struct mem_info *)aself;
if (self == 0)
{
return;
}
while (self->free_head != 0)
{
libmem_free_mem_item(self, self->free_head);
}
while (self->used_head != 0)
{
libmem_free_mem_item(self, self->used_head);
}
free(self);
2013-06-29 06:37:03 +04:00
}
/****************************************************************************/
static int
libmem_add_used_item(struct mem_info *self, unsigned int addr, int bytes)
2013-06-29 06:37:03 +04:00
{
struct mem_item *mi;
struct mem_item *new_mi;
int added;
2013-06-29 06:37:03 +04:00
if (self == 0 || addr == 0)
{
return 1;
}
if (self->used_head == 0)
2013-06-29 06:37:03 +04:00
{
/* add first item */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
2013-06-29 06:37:03 +04:00
self->used_head = new_mi;
self->used_tail = new_mi;
return 0;
2013-06-29 06:37:03 +04:00
}
added = 0;
mi = self->used_head;
while (mi != 0)
{
if (mi->addr > addr)
{
/* add before */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
new_mi->prev = mi->prev;
new_mi->next = mi;
if (mi->prev != 0)
{
mi->prev->next = new_mi;
}
mi->prev = new_mi;
if (self->used_head == mi)
{
self->used_head = new_mi;
}
added = 1;
break;
}
mi = mi->next;
}
if (!added)
{
/* add last */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
self->used_tail->next = new_mi;
new_mi->prev = self->used_tail;
self->used_tail = new_mi;
}
return 0;
2013-06-29 06:37:03 +04:00
}
/****************************************************************************/
static int
libmem_add_free_item(struct mem_info *self, unsigned int addr, int bytes)
2013-06-29 06:37:03 +04:00
{
struct mem_item *mi;
struct mem_item *new_mi;
int added;
2013-06-29 06:37:03 +04:00
if (self == 0 || addr == 0)
{
return 1;
}
if (self->free_head == 0)
{
/* add first item */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
self->free_head = new_mi;
self->free_tail = new_mi;
return 0;
}
added = 0;
mi = self->free_head;
while (mi != 0)
2013-06-29 06:37:03 +04:00
{
if (mi->addr > addr)
2013-06-29 09:11:15 +04:00
{
if (mi->prev != 0)
{
if (mi->prev->addr + mi->prev->bytes == addr)
{
/* don't need to add, just make prev bigger */
mi->prev->bytes += bytes;
if (mi->prev->addr + mi->prev->bytes == mi->addr)
{
/* here we can remove one */
mi->prev->bytes += mi->bytes;
libmem_free_mem_item(self, mi);
}
return 0;
}
}
if (addr + bytes == mi->addr)
{
/* don't need to add here either */
mi->addr = addr;
mi->bytes += bytes;
return 0;
}
/* add before */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
new_mi->prev = mi->prev;
new_mi->next = mi;
if (mi->prev != 0)
{
mi->prev->next = new_mi;
}
mi->prev = new_mi;
if (self->free_head == mi)
{
self->free_head = new_mi;
}
added = 1;
break;
2013-06-29 09:11:15 +04:00
}
mi = mi->next;
2013-06-29 06:37:03 +04:00
}
if (!added)
{
/* add last */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
self->free_tail->next = new_mi;
new_mi->prev = self->free_tail;
self->free_tail = new_mi;
}
return 0;
2013-06-29 06:37:03 +04:00
}
/*****************************************************************************/
static int
libmem_print(struct mem_info *self)
2013-06-29 06:37:03 +04:00
{
struct mem_item *mi;
2013-06-29 06:37:03 +04:00
LOG_DEVEL(LOG_LEVEL_DEBUG, "libmem_print:");
LOG_DEVEL(LOG_LEVEL_DEBUG, " used_head %p", self->used_head);
LOG_DEVEL(LOG_LEVEL_DEBUG, " used_tail %p", self->used_tail);
mi = self->used_head;
if (mi != 0)
2013-06-29 06:37:03 +04:00
{
LOG_DEVEL(LOG_LEVEL_DEBUG, " used list");
while (mi != 0)
{
LOG_DEVEL(LOG_LEVEL_DEBUG, " ptr %p prev %p next %p addr 0x%8.8x bytes %d",
mi, mi->prev, mi->next, mi->addr, mi->bytes);
mi = mi->next;
}
2013-06-29 06:37:03 +04:00
}
LOG_DEVEL(LOG_LEVEL_DEBUG, " free_head %p", self->free_head);
LOG_DEVEL(LOG_LEVEL_DEBUG, " free_tail %p", self->free_tail);
mi = self->free_head;
if (mi != 0)
2013-06-29 06:37:03 +04:00
{
LOG_DEVEL(LOG_LEVEL_DEBUG, " free list");
while (mi != 0)
{
LOG_DEVEL(LOG_LEVEL_DEBUG, " ptr %p prev %p next %p addr 0x%8.8x bytes %d",
mi, mi->prev, mi->next, mi->addr, mi->bytes);
mi = mi->next;
}
2013-06-29 06:37:03 +04:00
}
return 0;
2013-06-29 06:37:03 +04:00
}
/*****************************************************************************/
unsigned int
libmem_alloc(void *obj, int bytes)
2013-06-29 06:37:03 +04:00
{
struct mem_info *self;
struct mem_item *mi;
unsigned int addr;
2013-06-29 06:37:03 +04:00
if (bytes < 1)
2013-06-29 06:37:03 +04:00
{
return 0;
2013-06-29 06:37:03 +04:00
}
bytes = ALIGN(bytes);
self = (struct mem_info *)obj;
addr = 0;
mi = self->free_head;
while (mi != 0)
2013-06-29 06:37:03 +04:00
{
if (bytes <= mi->bytes)
{
addr = mi->addr;
mi->bytes -= bytes;
mi->addr += bytes;
if (mi->bytes < 1)
{
libmem_free_mem_item(self, mi);
}
break;
}
mi = mi->next;
}
if (addr != 0)
{
self->total_bytes += bytes;
libmem_add_used_item(self, addr, bytes);
if (self->flags & 1)
{
libmem_print(self);
}
}
else
{
LOG_DEVEL(LOG_LEVEL_ERROR, "libmem_alloc: error");
2013-06-29 06:37:03 +04:00
}
return addr;
2013-06-29 06:37:03 +04:00
}
/*****************************************************************************/
int
libmem_free(void *obj, unsigned int addr)
2013-06-29 06:37:03 +04:00
{
struct mem_info *self;
struct mem_item *mi;
2013-06-29 06:37:03 +04:00
if (addr == 0)
2013-06-29 06:37:03 +04:00
{
return 0;
2013-06-29 06:37:03 +04:00
}
self = (struct mem_info *)obj;
mi = self->used_tail;
while (mi != 0)
{
if (mi->addr == addr)
{
self->total_bytes -= mi->bytes;
libmem_add_free_item(self, mi->addr, mi->bytes);
libmem_free_mem_item(self, mi);
if (self->flags & 1)
{
libmem_print(self);
}
return 0;
}
mi = mi->prev;
}
LOG_DEVEL(LOG_LEVEL_ERROR, "libmem_free: error");
return 1;
2013-06-29 06:37:03 +04:00
}
/*****************************************************************************/
int
libmem_set_flags(void *obj, int flags)
2013-06-29 06:37:03 +04:00
{
struct mem_info *self;
2013-06-29 06:37:03 +04:00
self = (struct mem_info *)obj;
self->flags |= flags;
return 0;
2013-06-29 06:37:03 +04:00
}
/*****************************************************************************/
int
libmem_clear_flags(void *obj, int flags)
2013-06-29 06:37:03 +04:00
{
struct mem_info *self;
2013-06-29 06:37:03 +04:00
self = (struct mem_info *)obj;
self->flags &= ~flags;
return 0;
2013-06-29 06:37:03 +04:00
}
2013-08-24 04:03:55 +04:00
/*****************************************************************************/
int
libmem_get_alloced_bytes(void *obj)
2013-08-24 04:03:55 +04:00
{
struct mem_info *self;
2013-08-24 04:03:55 +04:00
self = (struct mem_info *)obj;
return self->total_bytes;
2013-08-24 04:03:55 +04:00
}