<commit_before>#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "malloc_hook.h"
#include "jemalloc.h"

#include "skynet.h"


static size_t _used_memory = 0;
static size_t _memory_block = 0;
typedef struct _mem_data {
    uint32_t handle;
    ssize_t allocated;
} mem_data;

#define SLOT_SIZE 0x10000
#define PREFIX_SIZE sizeof(uint32_t)

static mem_data mem_stats[SLOT_SIZE];

static size_t*
get_allocated_field(uint32_t handle) {
    int h = (int)(handle & (SLOT_SIZE - 1));
    mem_data *data = &mem_stats[h];
	uint32_t old_handle = data->handle;
	ssize_t old_alloc = data->allocated;
    if(old_handle == 0 || old_alloc <= 0) {
		// data->allocated may less than zero, because it may not count at start.
		if(!__sync_bool_compare_and_swap(&data->handle, old_handle, handle)) {
			return 0;
		}
		if (old_alloc < 0) {
			__sync_bool_compare_and_swap(&data->allocated, old_alloc, 0);
		}
    }
    if(data->handle != handle) {
        return 0;
    }
    return &data->allocated;
}

inline static void 
update_xmalloc_stat_alloc(uint32_t handle, size_t __n) {
    __sync_add_and_fetch(&_used_memory, __n);
    __sync_add_and_fetch(&_memory_block, 1); 
    size_t* allocated = get_allocated_field(handle);
    if(allocated) {
        __sync_add_and_fetch(allocated, __n);
    }
}

inline static void
update_xmalloc_stat_free(uint32_t handle, size_t __n) {
    __sync_sub_and_fetch(&_used_memory, __n);
    __sync_sub_and_fetch(&_memory_block, 1);
    size_t* allocated = get_allocated_field(handle);
    if(allocated) {
        __sync_sub_and_fetch(allocated, __n);
    }
}

inline static void*
fill_prefix(char* ptr) {
    uint32_t handle = skynet_current_handle();
    size_t size = je_malloc_usable_size(ptr);
    uint32_t *p = (uint32_t *)(ptr + size - sizeof(uint32_t));
    memcpy(p, &handle, sizeof(handle));

	update_xmalloc_stat_alloc(handle, size);<commit_msg>quickfix: ssize_t<commit_after>#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "malloc_hook.h"
#include "jemalloc.h"

#include "skynet.h"


static size_t _used_memory = 0;
static size_t _memory_block = 0;
typedef struct _mem_data {
    uint32_t handle;
    ssize_t allocated;
} mem_data;

#define SLOT_SIZE 0x10000
#define PREFIX_SIZE sizeof(uint32_t)

static mem_data mem_stats[SLOT_SIZE];

static ssize_t*
get_allocated_field(uint32_t handle) {
    int h = (int)(handle & (SLOT_SIZE - 1));
    mem_data *data = &mem_stats[h];
	uint32_t old_handle = data->handle;
	ssize_t old_alloc = data->allocated;
    if(old_handle == 0 || old_alloc <= 0) {
		// data->allocated may less than zero, because it may not count at start.
		if(!__sync_bool_compare_and_swap(&data->handle, old_handle, handle)) {
			return 0;
		}
		if (old_alloc < 0) {
			__sync_bool_compare_and_swap(&data->allocated, old_alloc, 0);
		}
    }
    if(data->handle != handle) {
        return 0;
    }
    return &data->allocated;
}

inline static void 
update_xmalloc_stat_alloc(uint32_t handle, size_t __n) {
    __sync_add_and_fetch(&_used_memory, __n);
    __sync_add_and_fetch(&_memory_block, 1); 
    ssize_t* allocated = get_allocated_field(handle);
    if(allocated) {
        __sync_add_and_fetch(allocated, __n);
    }
}

inline static void
update_xmalloc_stat_free(uint32_t handle, size_t __n) {
    __sync_sub_and_fetch(&_used_memory, __n);
    __sync_sub_and_fetch(&_memory_block, 1);
    ssize_t* allocated = get_allocated_field(handle);
    if(allocated) {
        __sync_sub_and_fetch(allocated, __n);
    }
}

inline static void*
fill_prefix(char* ptr) {
    uint32_t handle = skynet_current_handle();
    size_t size = je_malloc_usable_size(ptr);
    uint32_t *p = (uint32_t *)(ptr + size - sizeof(uint32_t));
    memcpy(p, &handle, sizeof(handle));

	update_xmalloc_stat_alloc(handle, size);
{'fraction_non_alphanumeric': 0.11645101663585952, 'fraction_numerical': 0.01954053340374967, 'mean_word_length': 4.380681818181818, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7427354', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1433, 'n_words': 288}