hol(pthread_mutex_t ** l1, pthread_mutex_t ** l2) { int i=10; if (i>10) *l1 = doMalloc(); else *l2 = doMalloc(); *l2 = &gl2; }
// Does the malloc for the last block in the linked list. // Ensures that there is a final block that is free and fills // the rest of the the heap void mallocForTailHeader(header *headerPointer, size_t size) { while (headerPointer->size <= headerSize + size) { if (sbrk(BREAK_INCREMENT) < 0) { } headerPointer->size += BREAK_INCREMENT; } doMalloc(headerPointer, size); }
foo() { int i = 0; printf("\n##### in foo #####\n\n"); struct rel * r; r = malloc(sizeof(struct rel)); l = & r->t; l1 = doMalloc(); printf("\n##### out of foo #####\n\n"); }
// Allocates a section of memory in the heap and returns // the pointer to the beginning of the block. There is always // a header describing the space remaining in the heap as free // at the end of the linked list of headers. void *myMalloc(size_t size) { if (size <= 0) { return NULL; } header *headerPointer = head; header *temp; if (headerPointer == NULL) { headerPointer = firstMalloc(size); } else { // While my header is not the last header and I am either not free // or too small, continue down the linked list while (headerPointer->next != NULL && !(headerPointer->freeFlag == TRUE && headerPointer->size >= size)) { headerPointer = headerPointer->next; } if (headerPointer->next == NULL) { mallocForTailHeader(headerPointer, size); } else { // If there is enough space for my size and the size of the next // header I need to make, use the space and make a new header to // divide the block if (headerPointer->size - size > headerSize) { doMalloc(headerPointer, size); } // Else use the original block with its size that might be a // little bigger than what was asked for else { headerPointer->freeFlag = FALSE; } } } // Align the pointer that is returned from malloc. This is safe to // do because the extra padding that is given is large enough to // allow the first bytes that block point to to be unused return (void *) ceil16((intptr_t) (headerPointer + 1)); }
int main() { doMalloc(); doIoctl(); return 0; }