utility.h revision f302905224a346718910e56f5f1593d4b19253f1
1322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * Use of this source code is governed by a BSD-style license that can be
3322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * found in the LICENSE file.
4322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
5322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
6322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Helper functions/wrappers for memory allocations, manipulation and
7322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * comparison.
8322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
9322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
10322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#ifndef VBOOT_REFERENCE_UTILITY_H_
11322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#define VBOOT_REFERENCE_UTILITY_H_
12322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
13f302905224a346718910e56f5f1593d4b19253f1Randall Spangler#include "sysincludes.h"
14322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
15ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah/* Outputs an error message and quits. */
16ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shahvoid error(const char *format, ...);
17ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah
18ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah/* Outputs debug/warning messages. */
19ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shahvoid debug(const char *format, ...);
20ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah
21ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah#define assert(expr) do { if (!(expr)) { \
22ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah      error("assert fail: %s at %s:%d\n", \
23ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah            #expr, __FILE__, __LINE__); }} while(0)
24ed9c96a7aa7f6493c94ef3ab3618e04def4a2b3cGaurav Shah
25ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and
26ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah * [lsw] forming the most and least signficant 16-bit words.
27ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah */
28ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define CombineUint16Pair(msw,lsw) (((msw) << 16) |     \
29ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah                                    (((lsw)) & 0xFFFF))
30ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah/* Return the minimum of (a) or (b). */
31ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah#define Min(a, b) (((a) < (b)) ? (a) : (b))
32ce0cc30e55987f3faac9f9bacdf5d66c86ede08eGaurav Shah
33322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Allocate [size] bytes and return a pointer to the allocated memory. Abort
34322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * on error.
35322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
36322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Malloc(size_t size);
37322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
38322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Free memory pointed by [ptr] previously allocated by Malloc(). */
39322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid Free(void* ptr);
40322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
4137f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo/* Compare [n] bytes in [src1] and [src2]
4237f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * Returns an integer less than, equal to, or greater than zero if the first [n]
4337f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * bytes of [src1] is found, respectively, to be less than, to match, or be
4437f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo * greater than the first n bytes of [src2]. */
4537f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Loint Memcmp(const void* src1, const void* src2, size_t n);
4637f6b55a25f337f555da1dfbe585d32cd004103dLouis Yung-Chieh Lo
47322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah/* Copy [n] bytes from [src] to [dest]. */
48322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahvoid* Memcpy(void* dest, const void* src, size_t n);
49322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
50d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah/* Set [n] bytes starting at [s] to [c]. */
51d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shahvoid* Memset(void *dest, const uint8_t c, size_t n);
52d067712ff9caaef6685ea147ba10a0a40f50222cGaurav Shah
5308df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah/* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match,
5408df9b88a36b7a351305a06b2849c5fcdac54135Gaurav Shah * 1 if they don't. Time taken to perform the comparison is only dependent on
55322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah * [n] and not on the relationship of the match between [s1] and [s2].
56322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah */
57322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shahint SafeMemcmp(const void* s1, const void* s2, size_t n);
58322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah
59d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson/* Ensure that only our stub implementations are used, not standard C */
60d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#ifndef _STUB_IMPLEMENTATION_
61d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define malloc _do_not_use_standard_malloc
62d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define free _do_not_use_standard_free
63d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcmp _do_not_use_standard_memcmp
64d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memcpy _do_not_use_standard_memcpy
65d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#define memset _do_not_use_standard_memset
66d6ff721eb077759ac8f672dc15094e65968bc45eBill Richardson#endif
67f5db4b86fa5569285dfc8373fd8977e1998b8066Bill Richardson
68f5db4b86fa5569285dfc8373fd8977e1998b8066Bill Richardson
69322536d2f9d30f42218cc9f2ab40574557da8a9Gaurav Shah#endif  /* VBOOT_REFERENCE_UTILITY_H_ */
70