1*dcdfe824SRobert Mustacchi /* 2*dcdfe824SRobert Mustacchi * This file and its contents are supplied under the terms of the 3*dcdfe824SRobert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0. 4*dcdfe824SRobert Mustacchi * You may only use this file in accordance with the terms of version 5*dcdfe824SRobert Mustacchi * 1.0 of the CDDL. 6*dcdfe824SRobert Mustacchi * 7*dcdfe824SRobert Mustacchi * A full copy of the text of the CDDL should have accompanied this 8*dcdfe824SRobert Mustacchi * source. A copy of the CDDL is also available via the Internet at 9*dcdfe824SRobert Mustacchi * http://www.illumos.org/license/CDDL. 10*dcdfe824SRobert Mustacchi */ 11*dcdfe824SRobert Mustacchi 12*dcdfe824SRobert Mustacchi /* 13*dcdfe824SRobert Mustacchi * Copyright 2016 Joyent, Inc. 14*dcdfe824SRobert Mustacchi */ 15*dcdfe824SRobert Mustacchi 16*dcdfe824SRobert Mustacchi /* 17*dcdfe824SRobert Mustacchi * Basic tests for aligned_alloc(3C). Note that we test ENOMEM failure by 18*dcdfe824SRobert Mustacchi * relying on the implementation of the current libc malloc. Specifically we go 19*dcdfe824SRobert Mustacchi * through and add a mapping so we can't expand the heap and then use it up. If 20*dcdfe824SRobert Mustacchi * the memory allocator is ever changed, this test will start failing, at which 21*dcdfe824SRobert Mustacchi * point, it may not be worth the cost of keeping it around. 22*dcdfe824SRobert Mustacchi */ 23*dcdfe824SRobert Mustacchi 24*dcdfe824SRobert Mustacchi #include <stdlib.h> 25*dcdfe824SRobert Mustacchi #include <errno.h> 26*dcdfe824SRobert Mustacchi #include <libproc.h> 27*dcdfe824SRobert Mustacchi #include <sys/sysmacros.h> 28*dcdfe824SRobert Mustacchi #include <sys/mman.h> 29*dcdfe824SRobert Mustacchi #include <sys/debug.h> 30*dcdfe824SRobert Mustacchi 31*dcdfe824SRobert Mustacchi int 32*dcdfe824SRobert Mustacchi main(void) 33*dcdfe824SRobert Mustacchi { 34*dcdfe824SRobert Mustacchi pstatus_t status; 35*dcdfe824SRobert Mustacchi void *buf; 36*dcdfe824SRobert Mustacchi 37*dcdfe824SRobert Mustacchi /* 38*dcdfe824SRobert Mustacchi * Alignment must be sizeof (void *) (word) aligned. 39*dcdfe824SRobert Mustacchi */ 40*dcdfe824SRobert Mustacchi VERIFY3P(aligned_alloc(sizeof (void *) - 1, 16), ==, NULL); 41*dcdfe824SRobert Mustacchi VERIFY3S(errno, ==, EINVAL); 42*dcdfe824SRobert Mustacchi 43*dcdfe824SRobert Mustacchi VERIFY3P(aligned_alloc(sizeof (void *) + 1, 16), ==, NULL); 44*dcdfe824SRobert Mustacchi VERIFY3S(errno, ==, EINVAL); 45*dcdfe824SRobert Mustacchi 46*dcdfe824SRobert Mustacchi 47*dcdfe824SRobert Mustacchi VERIFY3P(aligned_alloc(23, 16), ==, NULL); 48*dcdfe824SRobert Mustacchi VERIFY3S(errno, ==, EINVAL); 49*dcdfe824SRobert Mustacchi 50*dcdfe824SRobert Mustacchi buf = aligned_alloc(sizeof (void *), 16); 51*dcdfe824SRobert Mustacchi VERIFY3P(buf, !=, NULL); 52*dcdfe824SRobert Mustacchi free(buf); 53*dcdfe824SRobert Mustacchi 54*dcdfe824SRobert Mustacchi /* 55*dcdfe824SRobert Mustacchi * Cause ENOMEM 56*dcdfe824SRobert Mustacchi */ 57*dcdfe824SRobert Mustacchi VERIFY0(proc_get_status(getpid(), &status)); 58*dcdfe824SRobert Mustacchi VERIFY3P(mmap((caddr_t)P2ROUNDUP(status.pr_brkbase + 59*dcdfe824SRobert Mustacchi status.pr_brksize, 0x1000), 0x1000, 60*dcdfe824SRobert Mustacchi PROT_READ, MAP_ANON | MAP_FIXED | MAP_PRIVATE, -1, 0), 61*dcdfe824SRobert Mustacchi !=, (void *)-1); 62*dcdfe824SRobert Mustacchi 63*dcdfe824SRobert Mustacchi for (;;) { 64*dcdfe824SRobert Mustacchi if (malloc(16) == NULL) 65*dcdfe824SRobert Mustacchi break; 66*dcdfe824SRobert Mustacchi } 67*dcdfe824SRobert Mustacchi 68*dcdfe824SRobert Mustacchi for (;;) { 69*dcdfe824SRobert Mustacchi if (aligned_alloc(sizeof (void *), 16) == NULL) 70*dcdfe824SRobert Mustacchi break; 71*dcdfe824SRobert Mustacchi } 72*dcdfe824SRobert Mustacchi 73*dcdfe824SRobert Mustacchi VERIFY3P(aligned_alloc(sizeof (void *), 16), ==, NULL); 74*dcdfe824SRobert Mustacchi VERIFY3S(errno, ==, ENOMEM); 75*dcdfe824SRobert Mustacchi 76*dcdfe824SRobert Mustacchi return (0); 77*dcdfe824SRobert Mustacchi } 78