Skip to content
Snippets Groups Projects
Select Git revision
  • 33996685dff97fc070a19d6f30dc61019b26016d
  • wip-bootstrap default
  • dualcore
  • ch3/leds
  • ch3/time
  • master
6 results

objarray.c

Blame
  • misc.h 4.42 KiB
    // a mini library of useful types and functions
    
    #ifndef _INCLUDED_MINILIB_H
    #define _INCLUDED_MINILIB_H
    
    /** types *******************************************************/
    
    #include <stdbool.h>
    
    typedef unsigned char byte;
    typedef unsigned int uint;
    
    /** generic ops *************************************************/
    
    #ifndef MIN
    #define MIN(x, y) ((x) < (y) ? (x) : (y))
    #endif
    #ifndef MAX
    #define MAX(x, y) ((x) > (y) ? (x) : (y))
    #endif
    
    /** memomry allocation ******************************************/
    
    // TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
    
    #define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
    #define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
    #define m_new_obj(type) (m_new(type, 1))
    #define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
    #if MICROPY_ENABLE_FINALISER
    #define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type))))
    #else
    #define m_new_obj_with_finaliser(type) m_new_obj(type)
    #endif
    #define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
    #define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
    #define m_del_obj(type, ptr) (m_del(type, ptr, 1))
    #define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
    
    void *m_malloc(int num_bytes);
    void *m_malloc_maybe(int num_bytes);
    void *m_malloc_with_finaliser(int num_bytes);
    void *m_malloc0(int num_bytes);
    void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
    void m_free(void *ptr, int num_bytes);
    void *m_malloc_fail(int num_bytes);
    
    int m_get_total_bytes_allocated(void);
    int m_get_current_bytes_allocated(void);
    int m_get_peak_bytes_allocated(void);
    
    /** unichar / UTF-8 *********************************************/
    
    typedef int unichar; // TODO
    
    unichar utf8_get_char(const char *s);
    char *utf8_next_char(const char *s);
    
    bool unichar_isspace(unichar c);
    bool unichar_isalpha(unichar c);
    bool unichar_isprint(unichar c);
    bool unichar_isdigit(unichar c);
    bool unichar_isxdigit(unichar c);
    
    /** variable string *********************************************/
    
    typedef struct _vstr_t {
        uint alloc;
        uint len;
        char *buf;