C语言高级编程及实例部.rar
例1:本节给出三个非常实用和高速的内存分配函数。读者可以直接拷贝这三个函数到程序中去,使用Allocate()和My_Free()代替系统提供的alloc()和free()函数。调试环境为TC2.0或者TC3.0 /* FILE: ALLOCATE.C */ #define PRIVATE static /* Used to hide identifiers from BIND */ #define BLKSIZ 80 /* Size of allocation block */ #define STKSIZ 1000 /* Bytes reserved for stack */ #define MLCSIZ 3000 /* Bytes reserved for malloc() */ typedef union { char *link ; char data[BLKSIZ] ; } BLOCK ; PRIVATE char *btm_of_heap ; PRIVATE char *top_of_heap ; PRIVATE BLOCK *heap ; void Init_Heap() { unsigned *ptr ; unsigned size ; unsigned blk ; unsigned ttl_blks ; freeall(STKSIZ) ; /* Reserve a stack of 500 words */ ptr = _memory() + 1 ; /* Get pointer to heap size */ size = *ptr ; /* Get size of heap */ size -= MLCSIZ ; /* Keep 3K for malloc() */ ttl_blks = size/sizeof(BLOCK) ; /* # available blocks */ size = sizeof(BLOCK)*ttl_blks ; /* Use an even multiple */ btm_of_heap = malloc(size) ; /* Reserve the blocks */ heap = btm_of_heap ; /* Form the free list */ for (blk = 0 ; blk < ttl_blks - 1; blk++) { heap->link = heap + 1 ; heap++ ; } heap->link = 0 ; /* Establish end marker */ heap = btm_of_heap ; /* Restore head pointer */ top_of_heap = &heap[ttl_blks] ; /* 1st malloc() block */ } void My_Free(ptr) BLOCK *ptr ; { if (ptr >= btm_of_heap) { if (ptr < top_of_heap) /* Block is from my heap */ { ptr->link = heap ; heap = ptr ; return ; } else if (free(ptr)) /* Block is from C-Ware's heap */ { return ; } } puts("\nAttempt to free unallocated block!\n\7") ; exit(1) ; } char *Allocate(bytes) unsigned bytes ; { BLOCK *ptr ; if (bytes link ; return ptr ; } } if (ptr = malloc(bytes)) /* Won't fit or none left! */ { return ptr ; } puts("Insufficient memory: malloc()\n\7") ; exit(1) ; } ; unsigned size ; unsigned blk ; unsigned ttl_blks ; freeall(STKSIZ) ; /* Reserve a stack of 500 words */ ptr = _memory() + 1 ; /* Get pointer to heap size */ size = *ptr ; /* Get size of heap */ size -= MLCSIZ ; /* Keep 3K for malloc() */ ttl_blks = size/sizeof(BLOCK) ; /* # available blocks */ size = sizeof(BLOCK)*ttl_blks ; /* Use an even multiple */ btm_of_heap = malloc(size) ; /* Reserve the blocks */ heap = btm_of_heap ; /* Form the free list */ for (blk = 0 ; blk < ttl_blks - 1; blk++) { heap->link = heap + 1 ; heap++ ; } heap->link = 0 ; /* Establish end marker */ heap = btm_of_heap ; /* Restore head pointer */ top_of_heap = &heap[ttl_blks] ; /* 1st malloc() block */ } void My_Free(ptr) BLOCK *ptr ; { if (ptr >= btm_of_heap) { if (ptr < top_of_heap) /* Block is from my heap */ { ptr->link = heap ; heap = ptr ; return ; } else if (free(ptr)) /* Block is from C-Ware's heap */ { return ; } } puts("\nAttempt to free unallocated block!\n\7") ; exit(1) ; } char *Allocate(bytes) unsigned bytes ; { BLOCK *ptr ; if (bytes link ; return ptr ; } } if (ptr = malloc(bytes)) /* Won't fit or none left! */ { return ptr ; } puts("Insufficient memory: malloc()\n\7") ; exit(1) ; }
文件列表
C语言高级编程及实例部.rar
(预估有个172文件)
INT24.ASM
13KB
SIMUL101.ASM
14KB
FILE.C
46KB
WINDOW.C
26KB
UTILS.C
53KB
FINDREP.C
32KB
BLOCK.C
59KB
ED.C
45KB
EGAVGA.BGI
5KB
PRINTDOC.BAT
21B
暂无评论