📄 heap.c
字号:
/************************************************************
* *
* Copyright (c) 2001-2007 McObject LLC. All Right Reserved.*
* *
************************************************************/
/*
* This sample demonstarates heap API usage. The API resembles
* standard C runtime memory API, only every heap function takes
* an additional parameter - heap handle that should be obtained via
* mco_heap_init() call
*/
#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#ifndef _VXWORKS
#include <malloc.h>
#endif
#include "mco.h"
/* our application need 8K of dynamic memory, not that it MUST be
* a power of 2 in size. Try to set otherwise, and get the
* runtime fatal exception.
*/
#define HEAP_SIZE 8192
/* this is the starting address, could be just taken from the
* the memory map, or if the OS is kind enough, could be
* allocated with malloc
*/
void* start_address;
/* The address of the heap control structure must be allocated
* prior to the heap initialization and must be passed into
* the heap initialization API. Again this address could be
* taken from the map, or malloc-ed if possible
*/
mco_heap_h app_heap;
static void _SH_(void)
{
char text[] =
{
"\nThis sample demonstarates heap API usage. The API resembles\n"
"standard C runtime memory API, only every heap function takes\n"
"an additional parameter - heap handle that should be obtained via\n""mco_heap_init() call\n"
};
char text1[] =
{
"Copyright (c) 2001-2007 McObject LLC. All Right Reserved.\n\n"
};
printf("%s\n%s\nPress Enter to start", text, text1);
getchar();
}
/* If the address is static this is of course not necessary
*/
void free_start_address(void* p)
{
if (p)
{
free(p);
}
}
static void errhandler(int n)
{
printf("\neXtremeDB fatal error: %d", n);
getchar();
exit( - 1);
}
int main(void)
{
uint4* buffer;
uint4 size;
int hhsize;
_SH_();
/* Get the size of the heap control structure. The heap control structure
* address - heap header, must be provided by the application and
* passed into the heap initialization API mco_heap_init() along with
* the heap start address.
*/
mco_runtime_start();
hhsize = mco_heap_head_size();
if (!(app_heap = (mco_heap_h)malloc(hhsize)))
{
printf("Invalid address for the heap header.\n");
exit(0);
}
if (!(start_address = malloc(HEAP_SIZE)))
{
printf("Invalid head address\n");
exit(0);
}
/* set fatal error handler */
mco_error_set_handler(&errhandler);
/* initialize application's heap */
mco_heap_init(start_address, HEAP_SIZE, app_heap, "heap");
if ((buffer = (uint4*)mco_malloc(app_heap, 500* sizeof(uint4))) == NULL)
{
exit(1);
}
size = mco_heap_usedsize(app_heap);
printf("Size of block after mco_malloc of 500 longs: %u\n", size);
/* Reallocate and show new size: */
if ((buffer = mco_realloc(app_heap, buffer, size + (500* sizeof(uint4)))) == NULL)
{
exit(1);
}
size = mco_heap_usedsize(app_heap);
printf("Size of block after mco_realloc of 500 more longs: %u\n", size);
size = mco_heap_freesize(app_heap);
printf("Left in the heap after mco_malloc of 1000 longs: %u\n", size);
mco_free(app_heap, buffer);
/* destroy application's heap and free up its starting address (if necessary) */
mco_heap_destroy(app_heap);
mco_runtime_stop();
free(start_address);
free(app_heap);
printf("Press Enter to exit");
getchar();
PROG_EXIT(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -