10d163575Sopenharmony_ci/* 20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 40d163575Sopenharmony_ci * 50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 60d163575Sopenharmony_ci * are permitted provided that the following conditions are met: 70d163575Sopenharmony_ci * 80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 90d163575Sopenharmony_ci * conditions and the following disclaimer. 100d163575Sopenharmony_ci * 110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 120d163575Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 130d163575Sopenharmony_ci * provided with the distribution. 140d163575Sopenharmony_ci * 150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 160d163575Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 170d163575Sopenharmony_ci * permission. 180d163575Sopenharmony_ci * 190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 300d163575Sopenharmony_ci */ 310d163575Sopenharmony_ci 320d163575Sopenharmony_ci#include "stdlib.h" 330d163575Sopenharmony_ci#include "string.h" 340d163575Sopenharmony_ci#include "los_vm_map.h" 350d163575Sopenharmony_ci 360d163575Sopenharmony_ci/* 370d163575Sopenharmony_ci * Allocates the requested memory and returns a pointer to it. The requested 380d163575Sopenharmony_ci * size is nitems each size bytes long (total memory requested is nitems*size). 390d163575Sopenharmony_ci * The space is initialized to all zero bits. 400d163575Sopenharmony_ci */ 410d163575Sopenharmony_ci 420d163575Sopenharmony_civoid *calloc(size_t nitems, size_t size) 430d163575Sopenharmony_ci{ 440d163575Sopenharmony_ci size_t real_size; 450d163575Sopenharmony_ci void *ptr = NULL; 460d163575Sopenharmony_ci 470d163575Sopenharmony_ci if (nitems == 0 || size == 0) { 480d163575Sopenharmony_ci return NULL; 490d163575Sopenharmony_ci } 500d163575Sopenharmony_ci 510d163575Sopenharmony_ci real_size = (size_t)(nitems * size); 520d163575Sopenharmony_ci ptr = LOS_KernelMalloc((UINT32) real_size); 530d163575Sopenharmony_ci if (ptr != NULL) { 540d163575Sopenharmony_ci (void) memset_s((void *) ptr, real_size, 0, real_size); 550d163575Sopenharmony_ci } 560d163575Sopenharmony_ci return ptr; 570d163575Sopenharmony_ci} 580d163575Sopenharmony_ci 590d163575Sopenharmony_ci/* 600d163575Sopenharmony_ci * Deallocates the memory previously allocated by a call to calloc, malloc, or 610d163575Sopenharmony_ci * realloc. The argument ptr points to the space that was previously allocated. 620d163575Sopenharmony_ci * If ptr points to a memory block that was not allocated with calloc, malloc, 630d163575Sopenharmony_ci * or realloc, or is a space that has been deallocated, then the result is undefined. 640d163575Sopenharmony_ci */ 650d163575Sopenharmony_ci 660d163575Sopenharmony_civoid free(void *ptr) 670d163575Sopenharmony_ci{ 680d163575Sopenharmony_ci if (ptr == NULL) { 690d163575Sopenharmony_ci return; 700d163575Sopenharmony_ci } 710d163575Sopenharmony_ci 720d163575Sopenharmony_ci LOS_KernelFree(ptr); 730d163575Sopenharmony_ci} 740d163575Sopenharmony_ci 750d163575Sopenharmony_ci/* 760d163575Sopenharmony_ci * Allocates the requested memory and returns a pointer to it. The requested 770d163575Sopenharmony_ci * size is size bytes. The value of the space is indeterminate. 780d163575Sopenharmony_ci */ 790d163575Sopenharmony_ci 800d163575Sopenharmony_civoid *malloc(size_t size) 810d163575Sopenharmony_ci{ 820d163575Sopenharmony_ci if (size == 0) { 830d163575Sopenharmony_ci return NULL; 840d163575Sopenharmony_ci } 850d163575Sopenharmony_ci 860d163575Sopenharmony_ci return LOS_KernelMalloc((UINT32) size); 870d163575Sopenharmony_ci} 880d163575Sopenharmony_ci 890d163575Sopenharmony_civoid *zalloc(size_t size) 900d163575Sopenharmony_ci{ 910d163575Sopenharmony_ci void *ptr = NULL; 920d163575Sopenharmony_ci 930d163575Sopenharmony_ci if (size == 0) { 940d163575Sopenharmony_ci return NULL; 950d163575Sopenharmony_ci } 960d163575Sopenharmony_ci 970d163575Sopenharmony_ci ptr = LOS_KernelMalloc((UINT32) size); 980d163575Sopenharmony_ci if (ptr != NULL) { 990d163575Sopenharmony_ci (void) memset_s(ptr, size, 0, size); 1000d163575Sopenharmony_ci } 1010d163575Sopenharmony_ci return ptr; 1020d163575Sopenharmony_ci} 1030d163575Sopenharmony_ci 1040d163575Sopenharmony_ci/* 1050d163575Sopenharmony_ci * allocates a block of size bytes whose address is a multiple of boundary. 1060d163575Sopenharmony_ci * The boundary must be a power of two! 1070d163575Sopenharmony_ci */ 1080d163575Sopenharmony_ci 1090d163575Sopenharmony_civoid *memalign(size_t boundary, size_t size) 1100d163575Sopenharmony_ci{ 1110d163575Sopenharmony_ci if (size == 0) { 1120d163575Sopenharmony_ci return NULL; 1130d163575Sopenharmony_ci } 1140d163575Sopenharmony_ci 1150d163575Sopenharmony_ci return LOS_KernelMallocAlign((UINT32) size, (UINT32) boundary); 1160d163575Sopenharmony_ci} 1170d163575Sopenharmony_ci 1180d163575Sopenharmony_ci/* 1190d163575Sopenharmony_ci * Attempts to resize the memory block pointed to by ptr that was previously 1200d163575Sopenharmony_ci * allocated with a call to malloc or calloc. The contents pointed to by ptr are 1210d163575Sopenharmony_ci * unchanged. If the value of size is greater than the previous size of the 1220d163575Sopenharmony_ci * block, then the additional bytes have an undeterminate value. If the value 1230d163575Sopenharmony_ci * of size is less than the previous size of the block, then the difference of 1240d163575Sopenharmony_ci * bytes at the end of the block are freed. If ptr is null, then it behaves like 1250d163575Sopenharmony_ci * malloc. If ptr points to a memory block that was not allocated with calloc 1260d163575Sopenharmony_ci * or malloc, or is a space that has been deallocated, then the result is 1270d163575Sopenharmony_ci * undefined. If the new space cannot be allocated, then the contents pointed 1280d163575Sopenharmony_ci * to by ptr are unchanged. If size is zero, then the memory block is completely 1290d163575Sopenharmony_ci * freed. 1300d163575Sopenharmony_ci */ 1310d163575Sopenharmony_ci 1320d163575Sopenharmony_civoid *realloc(void *ptr, size_t size) 1330d163575Sopenharmony_ci{ 1340d163575Sopenharmony_ci if (ptr == NULL) { 1350d163575Sopenharmony_ci ptr = malloc(size); 1360d163575Sopenharmony_ci return ptr; 1370d163575Sopenharmony_ci } 1380d163575Sopenharmony_ci 1390d163575Sopenharmony_ci if (size == 0) { 1400d163575Sopenharmony_ci free(ptr); 1410d163575Sopenharmony_ci return NULL; 1420d163575Sopenharmony_ci } 1430d163575Sopenharmony_ci 1440d163575Sopenharmony_ci return LOS_KernelRealloc(ptr, (UINT32) size); 1450d163575Sopenharmony_ci} 146