11.3Sriastrad/* $NetBSD: aligned_alloc.c,v 1.3 2024/08/18 18:47:20 riastradh Exp $ */ 21.1Snros 31.1Snros/*- 41.1Snros * Copyright (C) 2015 The NetBSD Foundation, Inc. 51.1Snros * All rights reserved. 61.1Snros * 71.1Snros * This code is derived from software contributed to The NetBSD Foundation 81.1Snros * by Niclas Rosenvik. 91.1Snros * 101.1Snros * Redistribution and use in source and binary forms, with or without 111.1Snros * modification, are permitted provided that the following conditions 121.1Snros * are met: 131.1Snros * 1. Redistributions of source code must retain the above copyright 141.1Snros * notice(s), this list of conditions and the following disclaimer as 151.1Snros * the first lines of this file unmodified other than the possible 161.1Snros * addition of one or more copyright notices. 171.1Snros * 2. Redistributions in binary form must reproduce the above copyright 181.1Snros * notice(s), this list of conditions and the following disclaimer in 191.1Snros * the documentation and/or other materials provided with the 201.1Snros * distribution. 211.1Snros * 221.1Snros * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 231.1Snros * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241.1Snros * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 251.1Snros * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE 261.1Snros * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 271.1Snros * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 281.1Snros * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 291.1Snros * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 301.1Snros * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 311.1Snros * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 321.1Snros * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 331.1Snros */ 341.1Snros 351.3Sriastrad#include <sys/cdefs.h> 361.3Sriastrad__RCSID("$NetBSD: aligned_alloc.c,v 1.3 2024/08/18 18:47:20 riastradh Exp $"); 371.3Sriastrad 381.1Snros#include <errno.h> 391.1Snros#include <stdlib.h> 401.1Snros 411.1Snrosvoid * 421.1Snrosaligned_alloc(size_t alignment, size_t size) 431.1Snros{ 441.1Snros void *memptr; 451.1Snros int err; 461.3Sriastrad 471.1Snros /* 481.2Smaya * Check that alignment is a power of 2. 491.1Snros */ 501.2Smaya if (alignment == 0 || ((alignment - 1) & alignment) != 0) { 511.1Snros errno = EINVAL; 521.1Snros return NULL; 531.1Snros } 541.3Sriastrad 551.1Snros /* 561.1Snros * Adjust alignment to satisfy posix_memalign, 571.1Snros * larger alignments satisfy smaller alignments. 581.1Snros */ 591.1Snros while (alignment < sizeof(void *)) { 601.1Snros alignment <<= 1; 611.1Snros } 621.3Sriastrad 631.1Snros err = posix_memalign(&memptr, alignment, size); 641.1Snros if (err) { 651.1Snros errno = err; 661.1Snros return NULL; 671.1Snros } 681.3Sriastrad 691.1Snros return memptr; 701.1Snros} 71