| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stddef.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include "umock_c/umockalloc.h" |
| 8 |
|
| 9 |
void* umockalloc_malloc(size_t size) |
| 10 |
{ |
| 11 |
/* Codes_SRS_UMOCKALLOC_01_001: [ umockalloc_malloc shall call malloc, while passing the size argument to malloc. ] */ |
| 12 |
/* Codes_SRS_UMOCKALLOC_01_002: [ umockalloc_malloc shall return the result of malloc. ]*/ |
| 13 |
return malloc(size); |
| 14 |
} |
| 15 |
|
| 16 |
void* umockalloc_calloc(size_t nmemb, size_t size) |
| 17 |
{ |
| 18 |
/* Codes_SRS_UMOCKALLOC_09_001: [ umockalloc_calloc shall call calloc, while passing the number of members and size arguments to calloc. ] */ |
| 19 |
/* Codes_SRS_UMOCKALLOC_09_002: [ umockalloc_calloc shall return the result of calloc. ]*/ |
| 20 |
return calloc(nmemb, size); |
| 21 |
} |
| 22 |
|
| 23 |
void* umockalloc_realloc(void* ptr, size_t size) |
| 24 |
{ |
| 25 |
/* Codes_SRS_UMOCKALLOC_01_003: [ umockalloc_realloc shall call realloc, while passing the ptr and size arguments to realloc. ] */ |
| 26 |
/* Codes_SRS_UMOCKALLOC_01_004: [ umockalloc_realloc shall return the result of realloc. ]*/ |
| 27 |
return realloc(ptr, size); |
| 28 |
} |
| 29 |
|
| 30 |
void umockalloc_free(void* ptr) |
| 31 |
{ |
| 32 |
/* Codes_SRS_UMOCKALLOC_01_005: [ umockalloc_free shall call free, while passing the ptr argument to free. ]*/ |
| 33 |
free(ptr); |
| 34 |
} |
| 35 |
|
| 36 |
char* umockc_stringify_buffer(const void* bytes, size_t length) |
| 37 |
{ |
| 38 |
size_t string_length = 2 + (4 * length); |
| 39 |
char* result; |
| 40 |
if (length > 1) |
| 41 |
{ |
| 42 |
string_length += length - 1; |
| 43 |
} |
| 44 |
|
| 45 |
result = (char*)umockalloc_malloc(string_length + 1); |
| 46 |
if (result != NULL) |
| 47 |
{ |
| 48 |
size_t i; |
| 49 |
|
| 50 |
result[0] = '['; |
| 51 |
for (i = 0; i < length; i++) |
| 52 |
{ |
| 53 |
if (sprintf(result + 1 + (i * 5), "0x%02X ", ((const unsigned char*)bytes)[i]) < 0) |
| 54 |
{ |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if (i < length) |
| 60 |
{ |
| 61 |
umockalloc_free(result); |
| 62 |
result = NULL; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
result[string_length - 1] = ']'; |
| 67 |
result[string_length] = '\0'; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return result; |
| 72 |
} |