void OS_free (void * ptr) { malloc_header_t * header = (guarantee_valid_malloc_pointer (ptr)); APIRET rc; (header -> check) = 0; rc = (DosSubFreeMem (malloc_object, header, (header -> size))); if (rc != NO_ERROR) { char buffer [1024]; sprintf (buffer, "DosSubFreeMem error: %d.", rc); OS2_logic_error (buffer); } }
int main (VOID) { PVOID pvMemBase = NULL; /* Pointer to memory object (pool)*/ PVOID apvBlock[NUMBLOCKS] = {NULL}; /* Pointers to memory blocks */ ULONG i = 0; /* A loop index */ APIRET rc = NO_ERROR; /* Return code */ /* Allocate the memory pool. Note the pool is intentionally NOT committed here. OS/2 will commit the memory as needed. */ rc = DosAllocMem( &pvMemBase, POOLSIZE, PAG_WRITE ); if (rc != NO_ERROR) { printf("DosAllocMem error: return code = %u\n", rc); return 1; } /* Make the entire memory object available for Suballocation */ rc = DosSubSetMem( pvMemBase, DOSSUB_INIT | DOSSUB_SPARSE_OBJ, POOLSIZE ); if (rc != NO_ERROR) { printf("DosSubSetMem error: return code = %u\n", rc); return 1; } else { printf("Memory object ready for suballocation.\n"); } /* Suballocate the pool into 128 memory blocks, each 80 bytes in size */ for( i = 0; i < NUMBLOCKS; i++ ) { rc = DosSubAllocMem( pvMemBase, &apvBlock[ i ], 80 ); if( rc != NO_ERROR ) { printf( "DosSubAllocMem error: return code = %u (i = %u)\n", rc, i ); break; } } /* USE THE POOL HERE... */ /* When done, free the 128 memory blocks */ do { rc = DosSubFreeMem( pvMemBase, apvBlock[ --i ], 80 ); if (rc != NO_ERROR) { printf("DosSubFreeMem error: return code = %u (i = %u)\n", rc, i); return 1; } } while( i ); /* Stop memory block management, and free the memory object */ rc = DosSubUnsetMem( pvMemBase ); if (rc != NO_ERROR) { printf("DosSubUnsetMem error: return code = %u\n", rc); return 1; } rc = DosFreeMem( pvMemBase ); if (rc != NO_ERROR) { printf("DosFreeMem error: return code = %u\n", rc); return 1; } else { printf("Memory freed.\n"); } return NO_ERROR; }
static void heapDecrease(Heap_t uhp, PVOID p, size_t size) { logwrite("decreaseHeap - size : %8u\n", size, 0); DosSubFreeMem(pbasemem, p, size); }