Cloud Wind PBC Source ALLOC.C

Source: Internet
Author: User

#include <stdlib.h>#include<stdio.h>//the number of requests and releases used to count memory matchesStatic int_g =0;void*_pbcm_malloc (size_t sz) {++_g; return malloc(SZ);}void_pbcm_free (void*p) {if(p) {--_g;  Free(P); }}void* _PBCM_REALLOC (void*p, size_t sz) {    return realloc(P,SZ);}void_pbcm_memory () {printf ("%d\n", _g); }//linked list node for recording the first address of each piece of memorystructHeap_page {structHeap_page *next;};//The entry of the entire list, attaching the dimension information of the first Heap_page node, and subsequent nodes are not loggedstructHeap {structHeap_page *Current ; intsize; intused;};structHeap *_pbch_new (intpagesize) {    intCap =1024x768; //ensure cap is greater than pagesize and is a multiple of 1024     while(Cap <pagesize) {Cap*=2; }    //The dimensions in the heap structure record the Heap_page node that current points to    structHeap * H = (structHeap *) _pbcm_malloc (sizeof(structheap)); //here this memory request size sizeof (struct heap_page) + cap//The requested memory size is cap, but you need to append the list node to the head of the memory block that is sizeof (struct heap_page), so you can use Heap_page to string up each piece of memory//That is, each memory block is added to the head using the struct Heap_page structure in seriesH->current = (structHeap_page *) _pbcm_malloc (sizeof(structHeap_page) +cap); H->size =cap; H->used =0; H->current->next =NULL; returnh;}void_pbch_delete (structHeap *h) {//Traverse Heap_page, delete all    structHeap_page * p = h->Current ; structHeap_page * Next = p->Next;  for(;;)        {_pbcm_free (P); if(Next = =NULL) Break; P=Next; Next= p->Next; } _pbcm_free (h);}void*_pbch_alloc (structHeap *h,intsize) {    //the size obtained is greater than the incoming size and is a multiple of 4Size = (size +3) & ~3; //determine if the heap_page that the heap->current points to has enough memory space    if(H->size-h->used <size) {        structHeap_page *p; //Heap->size is the default size of each block of memory, if you can still request a heap->size size memory block        if(Size < H->size) {P= (structHeap_page *) _pbcm_malloc (sizeof(structHeap_page) + h->size); } Else{p= (structHeap_page *) _pbcm_malloc (sizeof(structHeap_page) +size); }        //inserting a newly created block of memory directly into the linked header, the heap also records only the newly created memory block, the original memory block is concatenated with the linked list, and can only wait for subsequent memory releasesP->next = h->Current ; H->current =p; H->used =size; //(p+1) is to point to real memory, not the heap_page structure of each memory head, P+1 is to move a heap_page space        return(p+1); } Else {        //return memory block unused part//(char *) (h->current + 1) is meant to point to true memory, not the heap_page structure of each memory header        Char* Buffer = (Char*) (H->current +1); Buffer+ = H->used; H->used + =size; returnbuffer; }}

Cloud Wind PBC Source ALLOC.C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.