<commit_before>/**
 * @file lv_mem.c
 * General and portable implementation of malloc and free.
 * The dynamic memory monitoring is also supported.
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_mem.h"
#include "lv_math.h"
#include <string.h>

#if LV_MEM_CUSTOM != 0
#include LV_MEM_CUSTOM_INCLUDE
#endif

/*********************
 *      DEFINES
 *********************/
#define LV_MEM_ADD_JUNK     0   /*Add memory junk on alloc (0xaa) and free(0xbb) (just for testing purposes)*/

// Check windows
#if _WIN32 || _WIN64
#if _WIN64
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif

// Check GCC
#if __GNUC__
#if __x86_64__ || __ppc64__
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif


#ifdef ENVIRONMENT64
#define MEM_UNIT uint64_t
#else
#define MEM_UNIT uint32_t
#endif

/**********************
 *      TYPEDEFS
 **********************/

/*The size of this union must be 4 bytes (uint32_t)*/
typedef union {
    struct {
    	MEM_UNIT used: 1;       //1: if the entry is used
    	MEM_UNIT d_size: 31;    //Size off the data (1 means 4 bytes)
    };
    MEM_UNIT header;            //The header (used + d_size)
} lv_mem_header_t;

typedef struct {
    lv_mem_header_t header;
    uint8_t first_data;        /*First data byte in the allocated data (Just for easily create a pointer)*/
} lv_mem_ent_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/
#if LV_MEM_CUSTOM == 0
static lv_mem_ent_t  * ent_get_next(lv_mem_ent_t * act_e);
static void * ent_alloc(lv_mem_ent_t * e, uint32_t size);
static void ent_trunc(lv_mem_ent_t * e, uint32_t size);
#endif

/**********************
 *  STATIC VARIABLES
 **********************/
#if LV_MEM_CUSTOM == 0
static LV_MEM_ATTR MEM_UNIT work_mem_int[LV_MEM_SIZE / sizeof(MEM_UNIT)];    /*Work memory for allocations*/
static uint8_t * work_mem;
#endif

static uint32_t zero_mem;       /*Give the address of this variable if 0 byte should be allocated*/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

/**
 * Initiaiize the dyn_mem module (work memory and other variables)
 */
void lv_mem_init(void)
{
#if LV_MEM_CUSTOM == 0
	work_mem = (uint8_t*) work_mem_int;
    lv_mem_ent_t * full = (lv_mem_ent_t *)work_mem;
    full->header.used = 0;
    /*The total mem size id reduced by the first header and the close patterns */
    full->header.d_size = LV_MEM_SIZE - sizeof(lv_mem_header_t);
#endif
}

/**
 * Allocate a memory dynamically
 * @param size size of the memory to allocate in bytes
 * @return pointer to the allocated memory
 */
void * lv_mem_alloc(uint32_t size)
{
    if(size == 0) {
        return &zero_mem;
    }

#ifdef ENVIRONMENT64
    /*Round the size up to 8*/
    if(size & 0x7) {
        size = size & (~0x7);
        size += 8;
    }
#else
    /*Round the size up to 4*/
    if(size & 0x3) {
        size = size & (~0x3);
        size += 4;
    }
#endif
    void * alloc = NULL;

#if LV_MEM_CUSTOM == 0 /*Use the allocation from dyn_mem*/
    lv_mem_ent_t * e = NULL;

    //Search for a appropriate entry
    do {
        //Get the next entry
        e = ent_get_next(e);
        printf("e size: %d\n", e->header.d_size);
        //If there is next entry then try to allocate there
        if(e != NULL) {
            alloc = ent_alloc(e, size);
        }
        //End if there is not next entry OR the alloc. is successful
    } while(e != NULL && alloc == NULL);

#if LV_MEM_ADD_JUNK
    if(alloc != NULL) memset(alloc, 0xaa, size);
#endif

#else  /*Use custom, user defined malloc function*/
    /*Allocate a header too to store the size*/
    alloc = LV_MEM_CUSTOM_ALLOC(size + sizeof(lv_mem_header_t));
    if(alloc != NULL) {
        ((lv_mem_ent_t *) alloc)->header.d_size = size;
        ((lv_mem_ent_t *) alloc)->header.used = 1;
        alloc = &((lv_mem_ent_t *) alloc)->first_data;
    }
#endif<commit_msg>lv_mem: remove debug printf<commit_after>/**
 * @file lv_mem.c
 * General and portable implementation of malloc and free.
 * The dynamic memory monitoring is also supported.
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_mem.h"
#include "lv_math.h"
#include <string.h>

#if LV_MEM_CUSTOM != 0
#include LV_MEM_CUSTOM_INCLUDE
#endif

/*********************
 *      DEFINES
 *********************/
#define LV_MEM_ADD_JUNK     0   /*Add memory junk on alloc (0xaa) and free(0xbb) (just for testing purposes)*/

// Check windows
#if _WIN32 || _WIN64
#if _WIN64
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif

// Check GCC
#if __GNUC__
#if __x86_64__ || __ppc64__
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif


#ifdef ENVIRONMENT64
#define MEM_UNIT uint64_t
#else
#define MEM_UNIT uint32_t
#endif

/**********************
 *      TYPEDEFS
 **********************/

/*The size of this union must be 4 bytes (uint32_t)*/
typedef union {
    struct {
    	MEM_UNIT used: 1;       //1: if the entry is used
    	MEM_UNIT d_size: 31;    //Size off the data (1 means 4 bytes)
    };
    MEM_UNIT header;            //The header (used + d_size)
} lv_mem_header_t;

typedef struct {
    lv_mem_header_t header;
    uint8_t first_data;        /*First data byte in the allocated data (Just for easily create a pointer)*/
} lv_mem_ent_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/
#if LV_MEM_CUSTOM == 0
static lv_mem_ent_t  * ent_get_next(lv_mem_ent_t * act_e);
static void * ent_alloc(lv_mem_ent_t * e, uint32_t size);
static void ent_trunc(lv_mem_ent_t * e, uint32_t size);
#endif

/**********************
 *  STATIC VARIABLES
 **********************/
#if LV_MEM_CUSTOM == 0
static LV_MEM_ATTR MEM_UNIT work_mem_int[LV_MEM_SIZE / sizeof(MEM_UNIT)];    /*Work memory for allocations*/
static uint8_t * work_mem;
#endif

static uint32_t zero_mem;       /*Give the address of this variable if 0 byte should be allocated*/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

/**
 * Initiaiize the dyn_mem module (work memory and other variables)
 */
void lv_mem_init(void)
{
#if LV_MEM_CUSTOM == 0
	work_mem = (uint8_t*) work_mem_int;
    lv_mem_ent_t * full = (lv_mem_ent_t *)work_mem;
    full->header.used = 0;
    /*The total mem size id reduced by the first header and the close patterns */
    full->header.d_size = LV_MEM_SIZE - sizeof(lv_mem_header_t);
#endif
}

/**
 * Allocate a memory dynamically
 * @param size size of the memory to allocate in bytes
 * @return pointer to the allocated memory
 */
void * lv_mem_alloc(uint32_t size)
{
    if(size == 0) {
        return &zero_mem;
    }

#ifdef ENVIRONMENT64
    /*Round the size up to 8*/
    if(size & 0x7) {
        size = size & (~0x7);
        size += 8;
    }
#else
    /*Round the size up to 4*/
    if(size & 0x3) {
        size = size & (~0x3);
        size += 4;
    }
#endif
    void * alloc = NULL;

#if LV_MEM_CUSTOM == 0 /*Use the allocation from dyn_mem*/
    lv_mem_ent_t * e = NULL;

    //Search for a appropriate entry
    do {
        //Get the next entry
        e = ent_get_next(e);

        /*If there is next entry then try to allocate there*/
        if(e != NULL) {
            alloc = ent_alloc(e, size);
        }
        //End if there is not next entry OR the alloc. is successful
    } while(e != NULL && alloc == NULL);

#if LV_MEM_ADD_JUNK
    if(alloc != NULL) memset(alloc, 0xaa, size);
#endif

#else  /*Use custom, user defined malloc function*/
    /*Allocate a header too to store the size*/
    alloc = LV_MEM_CUSTOM_ALLOC(size + sizeof(lv_mem_header_t));
    if(alloc != NULL) {
        ((lv_mem_ent_t *) alloc)->header.d_size = size;
        ((lv_mem_ent_t *) alloc)->header.used = 1;
        alloc = &((lv_mem_ent_t *) alloc)->first_data;
    }
#endif
{'fraction_non_alphanumeric': 0.17806828277700215, 'fraction_numerical': 0.01853027033887549, 'mean_word_length': 3.1517386722866174, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, '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': '1407229', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2743, 'n_words': 800}