1 // 2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd. 3 //Copyright (C) 2013 LunarG, Inc. 4 //All rights reserved. 5 // 6 //Redistribution and use in source and binary forms, with or without 7 //modification, are permitted provided that the following conditions 8 //are met: 9 // 10 // Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // 13 // Redistributions in binary form must reproduce the above 14 // copyright notice, this list of conditions and the following 15 // disclaimer in the documentation and/or other materials provided 16 // with the distribution. 17 // 18 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its 19 // contributors may be used to endorse or promote products derived 20 // from this software without specific prior written permission. 21 // 22 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 //POSSIBILITY OF SUCH DAMAGE. 34 // 35 /****************************************************************************\ 36 Copyright (c) 2002, NVIDIA Corporation. 37 38 NVIDIA Corporation("NVIDIA") supplies this software to you in 39 consideration of your agreement to the following terms, and your use, 40 installation, modification or redistribution of this NVIDIA software 41 constitutes acceptance of these terms. If you do not agree with these 42 terms, please do not use, install, modify or redistribute this NVIDIA 43 software. 44 45 In consideration of your agreement to abide by the following terms, and 46 subject to these terms, NVIDIA grants you a personal, non-exclusive 47 license, under NVIDIA's copyrights in this original NVIDIA software (the 48 "NVIDIA Software"), to use, reproduce, modify and redistribute the 49 NVIDIA Software, with or without modifications, in source and/or binary 50 forms; provided that if you redistribute the NVIDIA Software, you must 51 retain the copyright notice of NVIDIA, this notice and the following 52 text and disclaimers in all such redistributions of the NVIDIA Software. 53 Neither the name, trademarks, service marks nor logos of NVIDIA 54 Corporation may be used to endorse or promote products derived from the 55 NVIDIA Software without specific prior written permission from NVIDIA. 56 Except as expressly stated in this notice, no other rights or licenses 57 express or implied, are granted by NVIDIA herein, including but not 58 limited to any patent rights that may be infringed by your derivative 59 works or by other works in which the NVIDIA Software may be 60 incorporated. No hardware is licensed hereunder. 61 62 THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT 63 WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 64 INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE, 65 NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR 66 ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER 67 PRODUCTS. 68 69 IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, 70 INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 71 TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 72 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY 73 OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE 74 NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, 75 TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF 76 NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 77 \****************************************************************************/ 78 79 #include <stddef.h> 80 #include <stdlib.h> 81 #include <string.h> 82 #include <stdint.h> 83 84 #include "PpContext.h" 85 86 // default alignment and chunksize, if called with 0 arguments 87 #define CHUNKSIZE (64*1024) 88 #define ALIGN 8 89 90 namespace glslang { 91 92 struct chunk { 93 struct chunk *next; 94 }; 95 96 TPpContext::MemoryPool* TPpContext::mem_CreatePool(size_t chunksize, unsigned int align) 97 { 98 if (align == 0) 99 align = ALIGN; 100 if (chunksize == 0) 101 chunksize = CHUNKSIZE; 102 if (align & (align - 1)) 103 return nullptr; 104 if (chunksize < sizeof(MemoryPool)) 105 return nullptr; 106 if (chunksize & (align - 1)) 107 return nullptr; 108 109 MemoryPool *pool = (MemoryPool*)malloc(chunksize); 110 if (! pool) 111 return nullptr; 112 113 pool->next = 0; 114 pool->chunksize = chunksize; 115 pool->alignmask = (uintptr_t)(align) - 1; 116 pool->free = ((uintptr_t)(pool + 1) + pool->alignmask) & ~pool->alignmask; 117 pool->end = (uintptr_t)pool + chunksize; 118 119 return pool; 120 } 121 122 void TPpContext::mem_FreePool(MemoryPool *pool) 123 { 124 struct chunk *p, *next; 125 126 for (p = (struct chunk *)pool; p; p = next) { 127 next = p->next; 128 free(p); 129 } 130 } 131 132 void* TPpContext::mem_Alloc(MemoryPool *pool, size_t size) 133 { 134 struct chunk *ch; 135 void *rv = (void *)pool->free; 136 size = (size + pool->alignmask) & ~pool->alignmask; 137 if (size <= 0) size = pool->alignmask; 138 pool->free += size; 139 if (pool->free > pool->end || pool->free < (uintptr_t)rv) { 140 size_t minreq = (size + sizeof(struct chunk) + pool->alignmask) & ~pool->alignmask; 141 pool->free = (uintptr_t)rv; 142 if (minreq >= pool->chunksize) { 143 // request size is too big for the chunksize, so allocate it as 144 // a single chunk of the right size 145 ch = (struct chunk*)malloc(minreq); 146 if (! ch) 147 return nullptr; 148 } else { 149 ch = (struct chunk*)malloc(pool->chunksize); 150 if (! ch) 151 return nullptr; 152 pool->free = (uintptr_t)ch + minreq; 153 pool->end = (uintptr_t)ch + pool->chunksize; 154 } 155 ch->next = pool->next; 156 pool->next = ch; 157 rv = (void *)(((uintptr_t)(ch+1) + pool->alignmask) & ~pool->alignmask); 158 } 159 return rv; 160 } 161 162 } // end namespace glslang 163