Home | History | Annotate | Line # | Download | only in stdlib
t_posix_memalign.c revision 1.4.16.1
      1  1.4.16.1  christos /*	$NetBSD: t_posix_memalign.c,v 1.4.16.1 2019/06/10 22:10:05 christos Exp $ */
      2       1.1  pgoyette 
      3       1.1  pgoyette /*-
      4       1.1  pgoyette  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5       1.1  pgoyette  * All rights reserved.
      6       1.1  pgoyette  *
      7       1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  pgoyette  * by Christos Zoulas.
      9       1.1  pgoyette  *
     10       1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11       1.1  pgoyette  * modification, are permitted provided that the following conditions
     12       1.1  pgoyette  * are met:
     13       1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15       1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18       1.1  pgoyette  *
     19       1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  pgoyette  */
     31       1.1  pgoyette 
     32       1.1  pgoyette #include <sys/cdefs.h>
     33       1.1  pgoyette __COPYRIGHT("@(#) Copyright (c) 2008\
     34       1.1  pgoyette  The NetBSD Foundation, inc. All rights reserved.");
     35  1.4.16.1  christos __RCSID("$NetBSD: t_posix_memalign.c,v 1.4.16.1 2019/06/10 22:10:05 christos Exp $");
     36       1.1  pgoyette 
     37       1.1  pgoyette #include <atf-c.h>
     38       1.1  pgoyette 
     39       1.1  pgoyette #include <errno.h>
     40       1.3      nros #include <stdbool.h>
     41       1.1  pgoyette #include <stdint.h>
     42       1.1  pgoyette #include <stdio.h>
     43       1.1  pgoyette #include <stdlib.h>
     44       1.1  pgoyette #include <string.h>
     45       1.1  pgoyette 
     46       1.2    jruoho ATF_TC(posix_memalign_basic);
     47       1.2    jruoho ATF_TC_HEAD(posix_memalign_basic, tc)
     48       1.1  pgoyette {
     49       1.1  pgoyette 	atf_tc_set_md_var(tc, "descr", "Checks posix_memalign(3)");
     50       1.1  pgoyette }
     51       1.2    jruoho ATF_TC_BODY(posix_memalign_basic, tc)
     52       1.1  pgoyette {
     53       1.4      nros 	static const size_t size[] = {
     54       1.1  pgoyette 		1, 2, 3, 4, 10, 100, 16384, 32768, 65536
     55       1.1  pgoyette 	};
     56       1.4      nros 	static const size_t align[] = {
     57       1.1  pgoyette 		512, 1024, 16, 32, 64, 4, 2048, 16, 2
     58       1.1  pgoyette 	};
     59       1.1  pgoyette 
     60       1.1  pgoyette 	size_t i;
     61       1.1  pgoyette 	void *p;
     62       1.1  pgoyette 
     63       1.1  pgoyette 	for (i = 0; i < __arraycount(size); i++) {
     64       1.1  pgoyette 		int ret;
     65       1.1  pgoyette 		p = (void*)0x1;
     66       1.1  pgoyette 
     67       1.4      nros 		(void)printf("Checking posix_memalign(&p, %zu, %zu)...\n",
     68       1.1  pgoyette 			align[i], size[i]);
     69       1.1  pgoyette 		ret = posix_memalign(&p, align[i], size[i]);
     70       1.1  pgoyette 
     71       1.1  pgoyette 		if ( align[i] < sizeof(void *))
     72       1.1  pgoyette 			ATF_REQUIRE_EQ_MSG(ret, EINVAL,
     73       1.1  pgoyette 			    "posix_memalign: %s", strerror(ret));
     74       1.1  pgoyette 		else {
     75       1.1  pgoyette 			ATF_REQUIRE_EQ_MSG(ret, 0,
     76       1.1  pgoyette 			    "posix_memalign: %s", strerror(ret));
     77       1.1  pgoyette 			ATF_REQUIRE_EQ_MSG(((intptr_t)p) & (align[i] - 1), 0,
     78       1.1  pgoyette 			    "p = %p", p);
     79       1.1  pgoyette 			free(p);
     80       1.1  pgoyette 		}
     81       1.1  pgoyette 	}
     82       1.1  pgoyette }
     83       1.1  pgoyette 
     84       1.3      nros 
     85       1.3      nros ATF_TC(aligned_alloc_basic);
     86       1.3      nros ATF_TC_HEAD(aligned_alloc_basic, tc)
     87       1.3      nros {
     88       1.3      nros 	atf_tc_set_md_var(tc, "descr", "Checks aligned_alloc(3)");
     89       1.3      nros }
     90       1.3      nros ATF_TC_BODY(aligned_alloc_basic, tc)
     91       1.3      nros {
     92       1.3      nros 	static const size_t size[] = {
     93       1.3      nros 		1, 2, 3, 4, 10, 100, 16384, 32768, 65536, 10000, 0
     94       1.3      nros 	};
     95       1.3      nros 	static const size_t align[] = {
     96       1.3      nros 		512, 1024, 16, 32, 64, 4, 2048, 16, 2, 2048, 0
     97       1.3      nros 	};
     98       1.3      nros 
     99       1.3      nros 	size_t i;
    100       1.3      nros 	void *p;
    101       1.3      nros 
    102       1.3      nros 	for (i = 0; i < __arraycount(size); i++) {
    103       1.3      nros 		(void)printf("Checking aligned_alloc(%zu, %zu)...\n",
    104       1.3      nros 		    align[i], size[i]);
    105       1.3      nros 		p = aligned_alloc(align[i], size[i]);
    106       1.3      nros                 if (p == NULL) {
    107       1.4      nros 			if (align[i] == 0 || ((align[i] - 1) & align[i]) != 0 ||
    108       1.3      nros 			    size[i] % align[i] != 0) {
    109       1.3      nros 				ATF_REQUIRE_EQ_MSG(errno, EINVAL,
    110       1.3      nros 				    "aligned_alloc: %s", strerror(errno));
    111       1.3      nros 			}
    112       1.3      nros 			else {
    113       1.3      nros 				ATF_REQUIRE_EQ_MSG(errno, ENOMEM,
    114       1.3      nros 				    "aligned_alloc: %s", strerror(errno));
    115       1.3      nros 			}
    116       1.3      nros                 }
    117       1.3      nros 		else {
    118       1.3      nros 			ATF_REQUIRE_EQ_MSG(align[i] == 0, false,
    119       1.3      nros 			    "aligned_alloc: success when alignment was not "
    120       1.3      nros 			    "a power of 2");
    121       1.4      nros 			ATF_REQUIRE_EQ_MSG((align[i] - 1) & align[i], 0,
    122       1.3      nros 			    "aligned_alloc: success when alignment was not "
    123       1.3      nros 			    "a power of 2");
    124       1.3      nros 			ATF_REQUIRE_EQ_MSG(((intptr_t)p) & (align[i] - 1), 0,
    125       1.3      nros 			    "p = %p", p);
    126       1.3      nros 			free(p);
    127       1.3      nros 		}
    128       1.3      nros 	}
    129       1.3      nros }
    130       1.3      nros 
    131       1.3      nros 
    132       1.1  pgoyette ATF_TP_ADD_TCS(tp)
    133       1.1  pgoyette {
    134       1.2    jruoho 	ATF_TP_ADD_TC(tp, posix_memalign_basic);
    135       1.3      nros         ATF_TP_ADD_TC(tp, aligned_alloc_basic);
    136       1.3      nros 
    137       1.1  pgoyette 	return atf_no_error();
    138       1.1  pgoyette }
    139