Skip to content

Commit

Permalink
[shell/linux] avoid linking debug_heap for windows
Browse files Browse the repository at this point in the history
[shell/linux] add POLLRDHUP
  • Loading branch information
versaloon committed Jun 27, 2024
1 parent 51b6bda commit c276cba
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 26 deletions.
1 change: 1 addition & 0 deletions source/shell/sys/linux/include/poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef int nfds_t;
#define POLLRDBAND (1 << 7)
#define POLLWRNORM (1 << 8)
#define POLLWRBAND (1 << 9)
#define POLLRDHUP (1 << 10)

struct pollfd {
int fd;
Expand Down
144 changes: 119 additions & 25 deletions source/shell/sys/linux/lib/glibc/vsf_linux_glibc_stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ static void __vsf_linux_heap_trace_alloc(vsf_linux_process_t *process,
const char *file, const char *func, int line
)
{
if (NULL == process) {
process = vsf_linux_get_cur_process();
}
// if process is NULL, means not in linux environment, maybe allocating for C++ static instance
if (NULL == process) {
return;
}

node->ptr = ptr;
node->size = size;
# if VSF_LINUX_SIMPLE_STDLIB_CFG_HEAP_MONITOR_TRACE_CALLER == ENABLED
Expand All @@ -86,32 +78,45 @@ static void __vsf_linux_heap_trace_alloc(vsf_linux_process_t *process,
# endif
vsf_dlist_init_node(vsf_liunx_heap_node_t, node, node);

vsf_protect_t orig = vsf_protect_sched();
vsf_dlist_add_to_tail(vsf_liunx_heap_node_t, node, &process->heap_monitor.list, node);
process->heap_monitor.usage += size;
if (NULL == process) {
process = vsf_linux_get_cur_process();
}
if (process != NULL) {
vsf_protect_t orig = vsf_protect_sched();
vsf_dlist_add_to_tail(vsf_liunx_heap_node_t, node, &process->heap_monitor.list, node);
process->heap_monitor.usage += size;
# if VSF_LINUX_SIMPLE_STDLIB_HEAP_MONITOR_MAX == ENABLED
if (process->heap_monitor.usage > process->heap_monitor.max_usage) {
process->heap_monitor.max_usage = process->heap_monitor.usage;
}
if (process->heap_monitor.usage > process->heap_monitor.max_usage) {
process->heap_monitor.max_usage = process->heap_monitor.usage;
}
# endif
process->heap_monitor.balance++;
// vsf_trace_debug("0x%p: +%d 0x%p\n", process, node->size, ptr);
vsf_unprotect_sched(orig);
process->heap_monitor.balance++;
// vsf_trace_debug("0x%p: +%d 0x%p\n", process, node->size, ptr);
vsf_unprotect_sched(orig);
} else {
#ifdef __WIN__
// __vsf_arch_trace(0, "arch: +%d 0x%p\n", node->size, ptr);
#endif
}
}

static void __vsf_linux_heap_trace_free(vsf_linux_process_t *process, vsf_liunx_heap_node_t *node, void *ptr)
{
if (NULL == process) {
process = vsf_linux_get_cur_process();
}
VSF_LINUX_ASSERT(process != NULL);

vsf_protect_t orig = vsf_protect_sched();
// vsf_trace_debug("0x%p: -%d 0x%p\n", process, node->size, ptr);
process->heap_monitor.usage -= node->size;
process->heap_monitor.balance--;
vsf_dlist_remove(vsf_liunx_heap_node_t, node, &process->heap_monitor.list, node);
vsf_unprotect_sched(orig);
if (process != NULL) {
vsf_protect_t orig = vsf_protect_sched();
// vsf_trace_debug("0x%p: -%d 0x%p\n", process, node->size, ptr);
process->heap_monitor.usage -= node->size;
process->heap_monitor.balance--;
vsf_dlist_remove(vsf_liunx_heap_node_t, node, &process->heap_monitor.list, node);
vsf_unprotect_sched(orig);
} else {
#ifdef __WIN__
// __vsf_arch_trace(0, "arch: -%d 0x%p\n", node->size, ptr);
#endif
}
}

void __free_ex(vsf_linux_process_t *process, void *ptr)
Expand Down Expand Up @@ -842,6 +847,95 @@ void * calloc(size_t n, size_t size)
return __calloc_ex(NULL, n, size);
}

#ifdef __WIN__
// avoid linking ucrt.debug_heap

void _CrtDumpMemoryLeaks(void){}
void _CrtSetDbgFlag(void){}
void _CrtSetDumpClient(void){}

void *_malloc_dbg(size_t size, int blockType, const char *filename, int linenumber)
{
return malloc(size);
}

void * _calloc_dbg(size_t num, size_t size, int blockType, const char *filename, int linenumber)
{
size *= num;
void *result = _malloc_dbg(size, blockType, filename, linenumber);
if (result != 0) {
memset(result, 0, size);
}
return result;
}

void * _aligned_malloc_dbg(size_t size, size_t alignment, const char *filename, int linenumber)
{
return aligned_alloc(alignment, size);
}

void _free_dbg(void *p, int blockType)
{
free(p);
}

void _aligned_free_dbg(void *p)
{
free(p);
}

void * _aligned_offset_malloc_dbg(size_t size, size_t alignment, size_t offset, const char *filename, int linenumber)
{
return _aligned_malloc_dbg(size, alignment, filename, linenumber);
}

size_t _aligned_msize_dbg(void *p, size_t alignment, size_t offset)
{
return malloc_usable_size(p);
}

void * _realloc_dbg(void *p, size_t size, int blockType, const char *filename, int linenumber)
{
return realloc(p, size);
}

void * _aligned_realloc_dbg(void *p, size_t size, size_t alignment, const char *filename, int linenumber)
{
return realloc(p, size);
}

void * _aligned_offset_realloc_dbg(void *p, size_t size, size_t alignment, size_t offset, const char *filename, int linenumber)
{
return _aligned_realloc_dbg(p, size, alignment, filename, linenumber);
}

void * _recalloc_dbg(void *p, size_t num, size_t size, int blockType, const char *filename, int linenumber)
{
size *= num;
void *result = _realloc_dbg(p, size, blockType, filename, linenumber);
if (result != 0) {
memset(result, 0, size);
}
return result;
}

void * _aligned_recalloc_dbg(void *p, size_t num, size_t size, size_t alignment, const char *filename, int linenumber)
{
size *= num;
void *result = _aligned_realloc_dbg(p, size, alignment, filename, linenumber);
if (result != 0) {
memset(result, 0, size);
}
return result;
}

void * _aligned_offset_recalloc_dbg(void *p, size_t num, size_t size, size_t alignment, size_t offset, const char *filename, int linenumber)
{
return _aligned_recalloc_dbg(p, num * size, alignment, offset, filename, linenumber);
}

#endif

#if VSF_LINUX_APPLET_USE_LIBC_STDLIB == ENABLED && !defined(__VSF_APPLET__)

#if VSF_ARCH_USE_THREAD_REG == ENABLED
Expand Down
3 changes: 2 additions & 1 deletion source/shell/sys/linux/vsf_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,8 @@ void vsf_linux_process_heap_free(vsf_linux_process_t *process, void *buffer)
{
if (buffer != NULL) {
process = vsf_linux_get_real_process(process);
if (NULL == process->heap) {
// if process is NULL, means not in linux environment, maybe allocating for C++ static instance
if (NULL == process || NULL == process->heap) {
vsf_heap_free(buffer);
} else {
__vsf_heap_free(process->heap, buffer);
Expand Down

0 comments on commit c276cba

Please sign in to comment.