1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2008-2010 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci/*
30bf215546Sopenharmony_ci * Memory alignment wrappers.
31bf215546Sopenharmony_ci */
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#ifndef _OS_MEMORY_H_
35bf215546Sopenharmony_ci#error "Must not be included directly. Include os_memory.h instead"
36bf215546Sopenharmony_ci#endif
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci/**
40bf215546Sopenharmony_ci * Add two size_t values with integer overflow check.
41bf215546Sopenharmony_ci * TODO: leverage __builtin_add_overflow where available
42bf215546Sopenharmony_ci */
43bf215546Sopenharmony_cistatic inline bool
44bf215546Sopenharmony_ciadd_overflow_size_t(size_t a, size_t b, size_t *res)
45bf215546Sopenharmony_ci{
46bf215546Sopenharmony_ci   *res = a + b;
47bf215546Sopenharmony_ci   return *res < a || *res < b;
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci#if defined(HAVE_POSIX_MEMALIGN)
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic inline void *
54bf215546Sopenharmony_cios_malloc_aligned(size_t size, size_t alignment)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   void *ptr;
57bf215546Sopenharmony_ci   alignment = (alignment + sizeof(void*) - 1) & ~(sizeof(void*) - 1);
58bf215546Sopenharmony_ci   if(posix_memalign(&ptr, alignment, size) != 0)
59bf215546Sopenharmony_ci      return NULL;
60bf215546Sopenharmony_ci   return ptr;
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci#define os_free_aligned(_ptr) free(_ptr)
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci#else
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci/**
68bf215546Sopenharmony_ci * Return memory on given byte alignment
69bf215546Sopenharmony_ci */
70bf215546Sopenharmony_cistatic inline void *
71bf215546Sopenharmony_cios_malloc_aligned(size_t size, size_t alignment)
72bf215546Sopenharmony_ci{
73bf215546Sopenharmony_ci   char *ptr, *buf;
74bf215546Sopenharmony_ci   size_t alloc_size;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   /*
77bf215546Sopenharmony_ci    * Calculate
78bf215546Sopenharmony_ci    *
79bf215546Sopenharmony_ci    *   alloc_size = size + alignment + sizeof(void *)
80bf215546Sopenharmony_ci    *
81bf215546Sopenharmony_ci    * while checking for overflow.
82bf215546Sopenharmony_ci    */
83bf215546Sopenharmony_ci   if (add_overflow_size_t(size, alignment, &alloc_size) ||
84bf215546Sopenharmony_ci       add_overflow_size_t(alloc_size, sizeof(void *), &alloc_size)) {
85bf215546Sopenharmony_ci      return NULL;
86bf215546Sopenharmony_ci   }
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   ptr = (char *) os_malloc(alloc_size);
89bf215546Sopenharmony_ci   if (!ptr)
90bf215546Sopenharmony_ci      return NULL;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   buf = (char *)(((uintptr_t)ptr + sizeof(void *) + alignment - 1) & ~((uintptr_t)(alignment - 1)));
93bf215546Sopenharmony_ci   *(char **)(buf - sizeof(void *)) = ptr;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   return buf;
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci/**
100bf215546Sopenharmony_ci * Free memory returned by os_malloc_aligned().
101bf215546Sopenharmony_ci */
102bf215546Sopenharmony_cistatic inline void
103bf215546Sopenharmony_cios_free_aligned(void *ptr)
104bf215546Sopenharmony_ci{
105bf215546Sopenharmony_ci   if (ptr) {
106bf215546Sopenharmony_ci      void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
107bf215546Sopenharmony_ci      void *realAddr = *cubbyHole;
108bf215546Sopenharmony_ci      os_free(realAddr);
109bf215546Sopenharmony_ci   }
110bf215546Sopenharmony_ci}
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci#endif
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci/**
115bf215546Sopenharmony_ci * Reallocate memeory, with alignment
116bf215546Sopenharmony_ci */
117bf215546Sopenharmony_cistatic inline void *
118bf215546Sopenharmony_cios_realloc_aligned(void *ptr, size_t oldsize, size_t newsize, size_t alignment)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci   const size_t copySize = MIN2(oldsize, newsize);
121bf215546Sopenharmony_ci   void *newBuf = os_malloc_aligned(newsize, alignment);
122bf215546Sopenharmony_ci   if (newBuf && ptr && copySize > 0) {
123bf215546Sopenharmony_ci      memcpy(newBuf, ptr, copySize);
124bf215546Sopenharmony_ci   }
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   os_free_aligned(ptr);
127bf215546Sopenharmony_ci   return newBuf;
128bf215546Sopenharmony_ci}
129