malloc_test.cpp revision 72bbd423579bb971dc06cdd3c06201faf3fe95e6
1885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris/* 2885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * Copyright (C) 2013 The Android Open Source Project 3885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * 4885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * Licensed under the Apache License, Version 2.0 (the "License"); 5885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * you may not use this file except in compliance with the License. 6885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * You may obtain a copy of the License at 7885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * 8885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * http://www.apache.org/licenses/LICENSE-2.0 9885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * 10885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * Unless required by applicable law or agreed to in writing, software 11885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * distributed under the License is distributed on an "AS IS" BASIS, 12885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * See the License for the specific language governing permissions and 14885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris * limitations under the License. 15885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris */ 16885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 17885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris#include <gtest/gtest.h> 18885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 19885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris#include <stdlib.h> 20885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris#include <malloc.h> 21885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 22885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_std) { 23885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Simple malloc test. 24885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris void *ptr = malloc(100); 25885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 26885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 27885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 28885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 29885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 30885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 31885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_std) { 32885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Simple calloc test. 33885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris size_t alloc_len = 100; 34885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)calloc(1, alloc_len); 35885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 36885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(alloc_len, malloc_usable_size(ptr)); 37885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < alloc_len; i++) { 38885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 39885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 40885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 41885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 42885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 43885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 44885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, memalign_multiple) { 45885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Memalign test where the alignment is any value. 46885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i <= 12; i++) { 47885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t alignment = 1 << i; alignment < (1U << (i+1)); alignment++) { 48885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char*)memalign(alignment, 100); 4972bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris ASSERT_TRUE(ptr != NULL) << alignment; 50885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 51885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, (intptr_t)ptr % (1 << i)); 52885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 53885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 54885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 55885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 56885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 57885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 58885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, memalign_realloc) { 59885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Memalign and then realloc the pointer a couple of times. 60885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t alignment = 1; alignment <= 4096; alignment <<= 1) { 61885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char*)memalign(alignment, 100); 62885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 63885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 64885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0U, (intptr_t)ptr % alignment); 65885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x23, 100); 66885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 67885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 200); 68885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 69885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 70885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 71885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 72885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 73885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 74885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x45, 200); 75885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 76885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 300); 77885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 78885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(300U, malloc_usable_size(ptr)); 79885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 200; i++) { 80885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x45, ptr[i]); 81885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 82885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x67, 300); 83885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 84885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 250); 85885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 86885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(250U, malloc_usable_size(ptr)); 87885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 250; i++) { 88885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x67, ptr[i]); 89885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 90885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 91885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 92885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 93885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 94885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 95885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_realloc_larger) { 96885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Realloc to a larger size, malloc is used for the original allocation. 97885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)malloc(100); 98885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 99885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 100885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 67, 100); 101885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 102885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 200); 103885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 104885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 105885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 106885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(67, ptr[i]); 107885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 108885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 109885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 110885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 111885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 112885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_realloc_smaller) { 113885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Realloc to a smaller size, malloc is used for the original allocation. 114885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)malloc(200); 115885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 116885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 117885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 67, 200); 118885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 119885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 100); 120885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 121885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 122885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 123885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(67, ptr[i]); 124885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 125885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 126885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 127885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 128885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 129885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, malloc_multiple_realloc) { 130885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Multiple reallocs, malloc is used for the original allocation. 131885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)malloc(200); 132885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 133885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 134885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x23, 200); 135885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 136885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 100); 137885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 138885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 139885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 140885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 141885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 142885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 143885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 50); 144885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 145885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(50U, malloc_usable_size(ptr)); 146885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 50; i++) { 147885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 148885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 149885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 150885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 150); 151885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 152885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(150U, malloc_usable_size(ptr)); 153885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 50; i++) { 154885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 155885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 156885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0x23, 150); 157885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 158885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 425); 159885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 160885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(425U, malloc_usable_size(ptr)); 161885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 150; i++) { 162885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0x23, ptr[i]); 163885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 164885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 165885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 166885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 167885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_realloc_larger) { 168885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Realloc to a larger size, calloc is used for the original allocation. 169885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)calloc(1, 100); 170885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 171885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 172885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 173885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 200); 174885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 175885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 176885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 177885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 178885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 179885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 180885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 181885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 182885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 183885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_realloc_smaller) { 184885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Realloc to a smaller size, calloc is used for the original allocation. 185885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)calloc(1, 200); 186885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 187885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 188885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 189885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 100); 190885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 191885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 192885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 193885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 194885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 195885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 196885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 197885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 198885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 199885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher FerrisTEST(malloc, calloc_multiple_realloc) { 200885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris // Multiple reallocs, calloc is used for the original allocation. 201885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris char *ptr = (char *)calloc(1, 200); 202885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 203885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(200U, malloc_usable_size(ptr)); 204885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 205885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char *)realloc(ptr, 100); 206885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 207885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(100U, malloc_usable_size(ptr)); 208885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 100; i++) { 209885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 210885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 211885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 212885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 50); 213885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 214885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(50U, malloc_usable_size(ptr)); 215885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 50; i++) { 216885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 217885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 218885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 219885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 150); 220885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 221885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(150U, malloc_usable_size(ptr)); 222885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 50; i++) { 223885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 224885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 225885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris memset(ptr, 0, 150); 226885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 227885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ptr = (char*)realloc(ptr, 425); 228885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_TRUE(ptr != NULL); 229885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_LE(425U, malloc_usable_size(ptr)); 230885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris for (size_t i = 0; i < 150; i++) { 231885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris ASSERT_EQ(0, ptr[i]); 232885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris } 233885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris 234885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris free(ptr); 235885f3b9cad01b8158aadc55c159c17dbf34f622cChristopher Ferris} 23672bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris 23772bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher FerrisTEST(malloc, posix_memalign_non_power2) { 23872bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris void* ptr; 23972bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris 24072bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris ASSERT_EQ(EINVAL, posix_memalign(&ptr, 17, 1024)); 24172bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris} 24272bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris 24372bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher FerrisTEST(malloc, memalign_non_power2) { 24472bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris void* ptr; 24572bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris for (size_t align = 0; align <= 256; align++) { 24672bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris ptr = memalign(align, 1024); 24772bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris ASSERT_TRUE(ptr != NULL) << "Failed at align " << align; 24872bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris free(ptr); 24972bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris } 25072bbd423579bb971dc06cdd3c06201faf3fe95e6Christopher Ferris} 251