Allocate a sized block of memory. @param size The size of the memory block in bytes. @param flags Failure action modifiers (bitmasks). @return A pointer to the allocated memory block, or NULL on failure. */
| 26 | @return A pointer to the allocated memory block, or NULL on failure. |
| 27 | */ |
| 28 | void *my_malloc(size_t size, myf my_flags) |
| 29 | { |
| 30 | void* point; |
| 31 | DBUG_ENTER("my_malloc"); |
| 32 | DBUG_PRINT("my",("size: %lu my_flags: %d", (ulong) size, my_flags)); |
| 33 | |
| 34 | /* Safety */ |
| 35 | if (!size) |
| 36 | size=1; |
| 37 | |
| 38 | point= malloc(size); |
| 39 | DBUG_EXECUTE_IF("simulate_out_of_memory", |
| 40 | { |
| 41 | free(point); |
| 42 | point= NULL; |
| 43 | }); |
| 44 | DBUG_EXECUTE_IF("simulate_persistent_out_of_memory", |
| 45 | { |
| 46 | free(point); |
| 47 | point= NULL; |
| 48 | }); |
| 49 | |
| 50 | if (point == NULL) |
| 51 | { |
| 52 | my_errno=errno; |
| 53 | if (my_flags & MY_FAE) |
| 54 | error_handler_hook=fatal_error_handler_hook; |
| 55 | if (my_flags & (MY_FAE+MY_WME)) |
| 56 | my_error(EE_OUTOFMEMORY, MYF(ME_BELL + ME_WAITTANG + |
| 57 | ME_NOREFRESH + ME_FATALERROR),size); |
| 58 | DBUG_EXECUTE_IF("simulate_out_of_memory", |
| 59 | DBUG_SET("-d,simulate_out_of_memory");); |
| 60 | if (my_flags & MY_FAE) |
| 61 | exit(1); |
| 62 | } |
| 63 | else if (my_flags & MY_ZEROFILL) |
| 64 | memset(point, 0, size); |
| 65 | DBUG_PRINT("exit",("ptr: %p", point)); |
| 66 | DBUG_RETURN(point); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /** |