Blender  V3.3
mallocn_inline.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-2-Clause
2  * Copyright 2002-2017 Jason Evans <jasone@canonware.com>. All rights reserved.
3  * 2007-2012 Mozilla Foundation. All rights reserved.
4  * 2009-2017 Facebook, Inc. All rights reserved. */
5 
10 #ifndef __MALLOCN_INLINE_H__
11 #define __MALLOCN_INLINE_H__
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 MEM_INLINE bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
18 {
19  /* A size_t with its high-half bits all set to 1. */
20  const size_t high_bits = SIZE_MAX << (sizeof(size_t) * 8 / 2);
21  *result = a * b;
22 
23  if (UNLIKELY(*result == 0)) {
24  return (a == 0 || b == 0);
25  }
26 
27  /*
28  * We got a non-zero size, but we don't know if we overflowed to get
29  * there. To avoid having to do a divide, we'll be clever and note that
30  * if both A and B can be represented in N/2 bits, then their product
31  * can be represented in N bits (without the possibility of overflow).
32  */
33  return ((high_bits & (a | b)) == 0 || (*result / b == a));
34 }
35 
36 #ifdef __cplusplus
37 }
38 #endif
39 
40 #endif /* __MALLOCN_INLINE_H__ */
#define UNLIKELY(x)
MEM_INLINE bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
#define MEM_INLINE
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
#define SIZE_MAX
Definition: stdint.h:206