[ctypes-commit] ctypes/source malloc_closure.c,1.1,1.2
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2004-10-21 09:32:38
|
Update of /cvsroot/ctypes/ctypes/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4396 Modified Files: malloc_closure.c Log Message: Double linked chain of pages. Index: malloc_closure.c =================================================================== RCS file: /cvsroot/ctypes/ctypes/source/malloc_closure.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** malloc_closure.c 21 Oct 2004 08:06:00 -0000 1.1 --- malloc_closure.c 21 Oct 2004 09:32:27 -0000 1.2 *************** *** 4,7 **** --- 4,12 ---- #include "ctypes.h" + + #ifdef _DEBUG + #define MALLOC_CLOSURE_DEBUG + #endif + #ifdef MS_WIN32 #include <windows.h> *************** *** 16,20 **** --- 21,27 ---- typedef struct _tagpage { struct _tagpage *next; + struct _tagpage *prev; int count; + int used; ffi_closure closure[0]; } PAGE; *************** *** 32,36 **** pagesize = systeminfo.dwPageSize; } ! page = (PAGE *)VirtualAlloc(NULL, pagesize, --- 39,43 ---- pagesize = systeminfo.dwPageSize; } ! page = (PAGE *)VirtualAlloc(NULL, pagesize, *************** *** 74,115 **** } void FreeClosure(void *p) { PAGE *page = start; - PAGE *prev = NULL; ffi_closure *pcl = (ffi_closure *)p; int i; - int isfree; ! while(page) { ! isfree = 1; for (i = 0; i < page->count; ++i) { - if (page->closure[i].cif) - isfree = 0; if (&page->closure[i] == pcl) { page->closure[i].cif = NULL; ! return; } } ! if (isfree) { ! PAGE *tmp = page; ! /* unlink the current page from the chain */ ! if (prev) ! prev->next = page->next; ! else ! start = page->next; ! page = page->next; ! /* now, page can be freed */ ! free_page(tmp); ! #ifdef MALLOC_CLOSURE_DEBUG ! printf("FREEING page %p\n", tmp); ! #endif } else { ! prev = page; ! page = page->next; } } - Py_FatalError("ctypes: closure not found in heap"); } --- 81,123 ---- } + /******************************************************************/ + void FreeClosure(void *p) { PAGE *page = start; ffi_closure *pcl = (ffi_closure *)p; int i; ! while (page) { for (i = 0; i < page->count; ++i) { if (&page->closure[i] == pcl) { page->closure[i].cif = NULL; ! --page->used; ! goto done; } } ! page = page->next; ! } ! Py_FatalError("ctypes: closure not found in heap"); ! done: ! if (page->used == 0) { ! /* unlink the current page from the chain */ ! if (page->next) ! page->next->prev = page->prev; ! if (page->prev) { ! page->prev->next = page->next; } else { ! start = page->next; ! if (start) ! start->prev = NULL; } + + /* now, page can be freed */ + free_page(page); + #ifdef MALLOC_CLOSURE_DEBUG + printf("FREEING page %p\n", page); + #endif } } *************** *** 119,140 **** int i; ! if (page == NULL) ! start = page = get_page(); while(page) { ! for (i = 0; i < page->count; ++i) { ! if (page->closure[i].cif == NULL) { ! page->closure[i].cif = (ffi_cif *)-1; ! return &page->closure[i]; } } if (page->next) page = page->next; else { ! PAGE *next = get_page(); ! if (next == NULL) return NULL; ! page->next = next; ! page = next; } } --- 127,158 ---- int i; ! if (start == NULL) ! page = start = get_page(); while(page) { ! if (page->used < page->count) { ! /* This page has a free entry */ ! for (i = 0; i < page->count; ++i) { ! if (page->closure[i].cif == NULL) { ! page->closure[i].cif = (ffi_cif *)-1; ! ++page->used; ! return &page->closure[i]; ! } } + /* oops, where is it? */ + Py_FatalError("ctypes: use count on page is wrong"); } if (page->next) + /* try the next page, if there is one */ page = page->next; else { ! /* need a fresh page */ ! PAGE *new_page = get_page(); ! if (new_page == NULL) return NULL; ! /* insert into chain */ ! new_page->prev = page; ! page->next = new_page; ! page = new_page; } } |