tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: aligned_alloc c11 function
In article <20151024194245.58b090e0%NetBSD.org@localhost>,
Niclas Rosenvik <nros%NetBSD.org@localhost> wrote:
>Hi, I have already discussed this with other NetBSD developers
>but to make it public I wanted to send a mail to tech-userland.
>I will commit this if no one objects in 24 hours.
>
>Regards,
>Niclas Rosenvik
>
>New file
>src/lib/libc/stdlib/aligned_alloc.c
>
>/* $NetBSD$ */
>
>/*-
> * Copyright (C) 2015 The NetBSD Foundation, Inc.
> * All rights reserved.
> *
> * This code is derived from software contributed to The NetBSD
>Foundation
> * by Niclas Rosenvik.
> *
> * Redistribution and use in source and binary forms, with or without
> * modification, are permitted provided that the following conditions
> * are met:
> * 1. Redistributions of source code must retain the above copyright
> * notice(s), this list of conditions and the following disclaimer as
> * the first lines of this file unmodified other than the possible
> * addition of one or more copyright notices.
> * 2. Redistributions in binary form must reproduce the above copyright
> * notice(s), this list of conditions and the following disclaimer in
> * the documentation and/or other materials provided with the
> * distribution.
> *
> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND
>ANY
> * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
> * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
> * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
> * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
> * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> */
>
>#include <errno.h>
>#include <stdlib.h>
>
>void *
>aligned_alloc(size_t alignment, size_t size)
>{
> void *memptr;
> int err = 0;
Overinitialization.
>
> /*
> * Check that size is an integer multiple of alignment
> * using n mod m. (n-1 & n) equals 0 is n is a power of 2.
> * (n mod m) equals (n & m-1) when m is a power of 2.
> * Since alignment must be a power of 2 we can use this instead
> * of size % alignment != 0.
> */
>
> if (((alignment - 1) & alignment) != 0 ||
> (size & (alignment-1)) != 0) {
Whitespace.
> errno = EINVAL;
> return NULL;
> }
>
> err = posix_memalign(&memptr, alignment, size);
>
> if (err) {
> errno = err;
> return NULL;
> }
>
> return memptr;
>}
>+ATF_TC_BODY(aligned_alloc_basic, tc)
>+{
>+ size_t size[] = {
>+ 1, 2, 3, 4, 10, 100, 16384, 32768, 65536, 10000
>+ };
>+ size_t align[] = {
>+ 512, 1024, 16, 32, 64, 4, 2048, 16, 2, 2048
>+ };
static const.
>+ size_t i;
>+ void *p;
>+
>+ for (i = 0; i < __arraycount(size); i++) {
>+ p = (void*)0x1;
Whitespace.
>+
>+ (void)printf("Checking aligned_alloc(%zd, %zd)...\n",
>+ align[i], size[i]);
%zu
>+ p = aligned_alloc(align[i], size[i]);
>+
>+ if ( align[i] < sizeof(void *) || size[i] % align[i] != 0 )
Whitespace
>+ ATF_REQUIRE_EQ_MSG(errno, EINVAL,
>+ "aligned_alloc: %s", strerror(errno));
>+ else {
>+ ATF_REQUIRE_EQ_MSG(p == NULL, false,
>+ "aligned_alloc: %s", strerror(errno));
>+ ATF_REQUIRE_EQ_MSG(((intptr_t)p) & (align[i]-1), 0,
Whitespace
christos
Home |
Main Index |
Thread Index |
Old Index