Decoded: OpenTTD (2003) v1.8 (2018) Source file: alloc_func.hpp Line-by-line code walkthrough by MaiZure alloc_func.hpp defines type-safe memory allocation functions Original code: https://github.com/MaiZure/OpenTTD-1.8/blob/master/src/core/alloc_func.hpp 1 COMMENT (*) 2 BLANK (-) 3 * 4 * 5 * 6 * 7 * 8 * 9 - 10 * 11 - 12 Header guard 13 Header guard 14 - 15 * 16 * 17 * 18 * 19 * 20 * 21 - 22 Forward declaration of MallocError 23 Forward declaration of ReallocError 24 - 25 * 26 * 27 * 28 * 29 * 30 * 31 Defines CheckAllocationConstraints with two arguments: size and count 32 BLOCK START - CheckAllocationConstraints, Allocation error check 33 If too many elements are requested, throw an error 34 BLOCK END - CheckAllocationConstraints 35 - 36 * 37 * 38 * 39 * 40 * 41 * 42 Set up a function template with a type parameter 43 Defines CheckAllocationConstraints with one argument: size 44 BLOCK START - CheckAllocationConstraints, type-specific interface for two argument function of the same name (See line 31) 45 Wraps the two argument function with the included type 46 BLOCK END - CheckAlloctionConstraints 47 - 48 * 49 * 50 * 51 * 52 * 53 * 54 * 55 * 56 * 57 * 58 Set up a function template with a type parameter 59 Declares MallocT with one argument: number of elements to allocate 60 BLOCK START - MallocT, type-specific malloc 61 * 62 * 63 * 64 * 65 * 66 No elements, means no allocation so return NULL pointer 67 - 68 * 69 Verify that we have room for the allocation 70 - 71 malloc the requested memory block and cast to type 72 If the malloc failed, throw an error (no return) 73 malloc succeeded, return the pointer 74 BLOCK END - MallocT 75 - 76 * 77 * 78 * 79 * 80 * 81 * 82 * 83 * 84 * 85 * 86 Set up a function template with a type parameter 87 Declares CallocT with one argument: number of elements to allocate 88 BLOCK START - CallocT, type-specific calloc (zero init memory) 89 * 90 * 91 * 92 * 93 * 94 No elements, means no allocation so return NULL pointer 95 - (Why does CallocT not check for size overflow??) 96 calloc the requested memory block and cast to type 97 If the calloc failed, throw an error (no return) 98 calloc succeeded, return the pointer 99 BLOCK END - CallocT 100 - 101 * 102 * 103 * 104 * 105 * 106 * 107 * 108 * 109 * 110 * 111 * 112 Set up a function template with a type parameter 113 Defines ReallocT for every possible type. Requires a valid pointer of the same type and an element count 114 BLOCK START - ReallocT, Type-safe realloc() 115 * 116 * 117 * 118 * 119 * 120 If no elements requested... 121 Free this pointer since it's now a zero allocation 122 Return a NULL pointer 123 End check for number of elements 124 - 125 * 126 Verify that we have room for the allocation 127 - 128 realloc the requested memory block and cast to type 129 If the realloc failed, throw an error (no return) 130 realloc succeeded, return the pointer 131 BLOCK END - ReallocT 132 - 133 * 134 Define macro for AllocaM -- needs to be a macro for local scope 135 Verify that we have room for the allocation 136 Call alloca and cast to type 137 - 138 Header guard end