13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*------------------------------------------------------------------------- 23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Base Portability Library 33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ------------------------------------- 43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project 63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License"); 83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License. 93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at 103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * http://www.apache.org/licenses/LICENSE-2.0 123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software 143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS, 153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and 173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License. 183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*! 203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file 213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Memory management. 223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deMemory.h" 253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deInt32.h" 263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdio.h> 283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <assert.h> 293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdlib.h> 303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string.h> 313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if defined(DE_VALGRIND_BUILD) 333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# include <valgrind/valgrind.h> 343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# if defined(HAVE_VALGRIND_MEMCHECK_H) 353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# include <valgrind/memcheck.h> 363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# endif 373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif 383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 393c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_BEGIN_EXTERN_C 403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Allocate a chunk of memory. 433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param numBytes Number of bytes to allocate. 443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \return Pointer to the allocated memory (or null on failure). 453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 463c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid* deMalloc (int numBytes) 473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry void* ptr; 493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(numBytes > 0); 513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry ptr = malloc((size_t)numBytes); 533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if defined(DE_DEBUG) 553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* Trash memory in debug builds (if under Valgrind, don't make it think we're initializing data here). */ 563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (ptr) 583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry memset(ptr, 0xcd, numBytes); 593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if defined(DE_VALGRIND_BUILD) && defined(HAVE_VALGRIND_MEMCHECK_H) 613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (ptr && RUNNING_ON_VALGRIND) 623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry VALGRIND_MAKE_MEM_UNDEFINED(ptr, numBytes); 643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif 663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif 673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return ptr; 693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Allocate a chunk of memory and initialize it to zero. 733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param numBytes Number of bytes to allocate. 743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \return Pointer to the allocated memory (or null on failure). 753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 763c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid* deCalloc (int numBytes) 773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry void* ptr = deMalloc(numBytes); 793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (ptr) 803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deMemset(ptr, 0, numBytes); 813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return ptr; 823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 843c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid* deAlignedMalloc (int numBytes, int alignBytes) 853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry int ptrSize = sizeof(void*); 873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deUintptr origPtr = (deUintptr)deMalloc(numBytes + ptrSize + alignBytes); 883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (origPtr) 893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deUintptr alignedPtr = (deUintptr)deAlignPtr((void*)(origPtr + ptrSize), alignBytes); 913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deUintptr ptrPtr = (alignedPtr - ptrSize); 923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(deInRange32(alignBytes, 0, 256) && deIsPowerOfTwo32(alignBytes)); 933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *(deUintptr*)ptrPtr = origPtr; 943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return (void*)alignedPtr; 953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry else 973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_NULL; 983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Reallocate a chunk of memory. 1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param ptr Pointer to previously allocated memory block 1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param numBytes New size in bytes 1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \return Pointer to the reallocated (and possibly moved) memory block 1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid* deRealloc (void* ptr, int numBytes) 1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return realloc(ptr, (size_t)numBytes); 1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Free a chunk of memory. 1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param ptr Pointer to memory to free. 1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deFree (void* ptr) 1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry free(ptr); 1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deAlignedFree (void* ptr) 1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (ptr) 1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry int ptrSize = sizeof(void*); 1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deUintptr ptrPtr = (deUintptr)ptr - ptrSize; 1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deUintptr origPtr = *(deUintptr*)ptrPtr; 1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(ptrPtr - origPtr < 256); 1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deFree((void*)origPtr); 1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyrychar* deStrdup (const char* str) 1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if (DE_COMPILER == DE_COMPILER_MSC) 1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return _strdup(str); 1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#elif (DE_OS == DE_OS_OSX) || (DE_OS == DE_OS_IOS) 1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* For some reason Steve doesn't like stdrup(). */ 1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry size_t len = strlen(str); 1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry char* copy = malloc(len+1); 1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry memcpy(copy, str, len); 1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry copy[len] = 0; 1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return copy; 1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else 1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return strdup(str); 1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif 1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1483c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_END_EXTERN_C 149