设为首页 加入收藏

TOP

自制编程语言crowbar(v0.1)构建解析器时分配内存(一)
2017-10-12 17:39:25 】 浏览:7150
Tags:自制 编程语言 crowbar v0.1 构建 解析 分配 内存

crowbar中第一次申请内存是在生成解析器的时候:

/* interface.c */
CRB_Interpreter *CRB_create_interpreter(void) { MEM_Storage storage; CRB_Interpreter *interpreter; storage = MEM_open_storage(0); interpreter = MEM_storage_malloc(storage, sizeof(struct CRB_Interpreter_tag)); interpreter->interpreter_storage = storage; interpreter->execute_storage = NULL; interpreter->variable = NULL; interpreter->function_list = NULL; interpreter->statement_list = NULL; interpreter->current_line_number = 1; crb_set_current_interpreter(interpreter); add_native_functions(interpreter); return interpreter; }

首先看一下MEM_Storage类型,声明:

/* MEM.h */
typedef struct MEM_Storage_tag *MEM_Storage;
/* MEM/storage.c */
struct MEM_Storage_tag {
    MemoryPageList      page_list;
    int                 current_page_size;
};

typedef MemoryPage *MemoryPageList;

typedef struct MemoryPage_tag MemoryPage;

struct MemoryPage_tag {
    int                 cell_num;
    int                 use_cell_num;
    MemoryPageList      next;
    Cell                cell[1];
};

typedef union {
    long        l_dummy;
    double      d_dummy;
    void        *p_dummy;
} Cell;

结构图:(cell是union)

再接着看MEM_open_storage(0)

/* MEM.h */
#define
MEM_open_storage(page_size)(MEM_open_storage_func(MEM_CURRENT_CONTROLLER, __FILE__, __LINE__, page_size))
/* MEM/storage.c */
MEM_Storage MEM_open_storage_func(MEM_Controller controller,char *filename, int line, int page_size)
{
    MEM_Storage storage;

    storage = MEM_malloc_func(controller, filename, line,
                              sizeof(struct MEM_Storage_tag));
    storage->page_list = NULL;
    assert(page_size >= 0);
    if (page_size > 0) {
        storage->current_page_size = page_size;
    } else {
        storage->current_page_size = DEFAULT_PAGE_SIZE;
    }

    return storage;
}
MEM_open_storage函数传了一个MEM_CURRENT_CONTROLLER宏,那接下来看一下这个宏定义:
/* MEM.h */
typedef struct MEM_Controller_tag *MEM_Controller;

extern MEM_Controller mem_default_controller;

#ifdef MEM_CONTROLLER
#define MEM_CURRENT_CONTROLLER MEM_CONTROLLER #else /* MEM_CONTROLLER */ #define MEM_CURRENT_CONTROLLER mem_default_controller #endif /* MEM_CONTROLLER */

 MEM_Controller_tag的定义:

/* MEM/memory.h */
typedef union Header_tag Header;
struct MEM_Controller_tag { FILE *error_fp; MEM_ErrorHandler error_handler; MEM_FailMode fail_mode; Header *block_header; };

/* MEM.h */
typedef void (*MEM_ErrorHandler)(MEM_Controller, char *, int, char *);

typedef enum {
    MEM_FAIL_AND_EXIT,
    MEM_FAIL_AND_RETURN
} MEM_FailMode;

  其中Header_tag定义:

/* MEM/memory.c */
union Header_tag {
    HeaderStruct        s;
    Align               u[HEADER_ALIGN_SIZE];
};

#define MARK_SIZE       (4)
#define ALIGN_SIZE      (sizeof(Align))
#define reva lue_up_align(val) ((val) ? (((val) - 1) / ALIGN_SIZE + 1) : 0) #define HEADER_ALIGN_SIZE (reva lue_up_align(sizeof(HeaderStruct)))
#define MARK (0xCD)
typedef struct { int size; char *filename; int line; Header *prev; Header *next; unsigned char mark[MARK_SIZE]; } HeaderStruct; typedef union { long l_dummy; double d_dummy; void *p_dummy; } Align;


可以看到在MEM_open_storage_func()中调用了MEM_malloc_func(),MEM_malloc_func原型:

/* MEM/memory.c */
void
*MEM_malloc_func(MEM_Controller controller,
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇172. Factorial Trailing Zeroes 下一篇C语言程序设计50例(一)(经典收藏)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目