Embedded Systems Design - August 2008 - (Page 38) 0808esd.p35to42 7/17/08 11:57 AM Page 38 20 years ago Listing 1 CALLOC.C. /* * Produced by Programming ARTS * 8/18/88 * Programmers: Les Aldridge, Travis I. Seay */ #define NULL (void *)0 typedef struct hdr { struct hdr unsigned int } HEADER; *ptr; size; /* Defined in the linker file. _heapstart is the first byte allocated to the heap; _heapend is the last. */ extern HEADER _heapstart, _heapend; extern void static HEADER static short warm_boot(char *str); *frhd; memleft; /* memory left */ void free(char *ap) { /* Return memory to free list. Where possible, make contiguous blocks of free memory. (Assumes that 0 is not a valid address for allocation. Also, i_alloc() must be called prior to using either free() or malloc(); otherwise, the free list will be null.) */ HEADER *nxt, *prev, *f; f = (HEADER *)ap - 1; memleft += f->size; /* Point to header of block being returned. */ /* frhd is never null unless i_alloc() wasn't called to initialize package. */ if (frhd > f) { /* Free-space head is higher up in memory than returnee. */ nxt = frhd; frhd = f; prev = f + f->size; if (prev==nxt) f->size += nxt->size; f->ptr = nxt->ptr; } else f->ptr = nxt; return; } Listing continues on page 40. /* old head */ /* new head */ /* right after new head */ /* Old and new are contiguous. */ /* Form one block. */ The application must call i_alloc() prior to calling malloc() or free(). Normally called from main(), i_alloc() initializes the free-space pointer to indicate the first available byte of heap space. (By convention, heap generally refers to the pool of memory available for dynamic allocation at run-time.) The free-space pointer initially indicates a header showing that the entire heap area is free and no other free blocks exist (see 38 Figure 1). The size of the free space is calculated in HEADER-size units. When pointers to a structure are added or subtracted in C, the result is divided by the size of the structure. For example, if _heapstart and _heapend differ by 4,096 bytes and the size of the structure HEADER is four bytes, the result of the pointer arithmetic is 1,024. The available space can be thought of as an array of header blocks and the calculation as finding the number of entries in the array. ALLOCATING & FREEING MEMORY Malloc() calculates the number of HEADER-size units required to satisfy the request. It rounds the result up and adds one unit for the header, which is part of the block allocated. For speed, the calculation could contain a shift instead of a divide. AUGUST 2008 | embedded systems design | www.embedded.com http://www.embedded.com
Table of Contents Feed for the Digital Edition of Embedded Systems Design - August 2008 Embedded Systems Design - August 2008 Contents Number Include Parity Bit Programmer's Toolbox Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications Wanted: Industry Standards for Benchmarking Embedded VMM Hypervisors Achieving Cache Coherence in a MIPS32 Multicore Design Memory Allocation in C Advertising Index Break Points Marketplace Embedded Systems Design - August 2008 Embedded Systems Design - August 2008 - Embedded Systems Design - August 2008 (Page Cover1) Embedded Systems Design - August 2008 - Embedded Systems Design - August 2008 (Page Cover2) Embedded Systems Design - August 2008 - Embedded Systems Design - August 2008 (Page 1) Embedded Systems Design - August 2008 - Embedded Systems Design - August 2008 (Page 2) Embedded Systems Design - August 2008 - Contents (Page 3) Embedded Systems Design - August 2008 - Contents (Page 4) Embedded Systems Design - August 2008 - Number Include (Page 5) Embedded Systems Design - August 2008 - Number Include (Page 6) Embedded Systems Design - August 2008 - Number Include (Page 7) Embedded Systems Design - August 2008 - Number Include (Page 8) Embedded Systems Design - August 2008 - Parity Bit (Page 9) Embedded Systems Design - August 2008 - Parity Bit (Page 10) Embedded Systems Design - August 2008 - Programmer's Toolbox (Page 11) Embedded Systems Design - August 2008 - Programmer's Toolbox (Page 12) Embedded Systems Design - August 2008 - Programmer's Toolbox (Page 13) Embedded Systems Design - August 2008 - Programmer's Toolbox (Page 14) Embedded Systems Design - August 2008 - Programmer's Toolbox (Page 15) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 16) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 17) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 18) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 19) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 20) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 21) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 22) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 23) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 24) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 25) Embedded Systems Design - August 2008 - Cover Feature: Virtualization for Embedded X86 Multiprocessor Applications (Page 26) Embedded Systems Design - August 2008 - Wanted: Industry Standards for Benchmarking Embedded VMM Hypervisors (Page 27) Embedded Systems Design - August 2008 - Wanted: Industry Standards for Benchmarking Embedded VMM Hypervisors (Page 28) Embedded Systems Design - August 2008 - Wanted: Industry Standards for Benchmarking Embedded VMM Hypervisors (Page 29) Embedded Systems Design - August 2008 - Achieving Cache Coherence in a MIPS32 Multicore Design (Page 30) Embedded Systems Design - August 2008 - Achieving Cache Coherence in a MIPS32 Multicore Design (Page 31) Embedded Systems Design - August 2008 - Achieving Cache Coherence in a MIPS32 Multicore Design (Page 32) Embedded Systems Design - August 2008 - Achieving Cache Coherence in a MIPS32 Multicore Design (Page 33) Embedded Systems Design - August 2008 - Achieving Cache Coherence in a MIPS32 Multicore Design (Page 34) Embedded Systems Design - August 2008 - Memory Allocation in C (Page 35) Embedded Systems Design - August 2008 - Memory Allocation in C (Page 36) Embedded Systems Design - August 2008 - Memory Allocation in C (Page 37) Embedded Systems Design - August 2008 - Memory Allocation in C (Page 38) Embedded Systems Design - August 2008 - Memory Allocation in C (Page 39) Embedded Systems Design - August 2008 - Memory Allocation in C (Page 40) Embedded Systems Design - August 2008 - Memory Allocation in C (Page 41) Embedded Systems Design - August 2008 - Memory Allocation in C (Page 42) Embedded Systems Design - August 2008 - Advertising Index (Page 43) Embedded Systems Design - August 2008 - Advertising Index (Page 44) Embedded Systems Design - August 2008 - Break Points (Page 45) Embedded Systems Design - August 2008 - Break Points (Page 46) Embedded Systems Design - August 2008 - Marketplace (Page 47) Embedded Systems Design - August 2008 - Marketplace (Page 48) Embedded Systems Design - August 2008 - Marketplace (Page Cover3) Embedded Systems Design - August 2008 - Marketplace (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.