4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Much like calloc, but with functions to report the size of the
29 * allocation given only the pointer.
35 #include "sized_array.h"
38 * Assumes that int is at least 32 bits and that nothing needs more than
42 /* COOKIE provides some bad-pointer protection. */
43 #define COOKIE "SACOOKIE"
55 sized_array(size_t n
, size_t sz
)
57 struct sized_array
*sa
;
60 total
= sizeof (struct sized_array
) + n
*sz
;
67 (void) memset(sa
, 0, total
);
73 (void) memcpy(sa
->cookie
, COOKIE
, sizeof (sa
->cookie
));
76 return ((void *)(sa
+ 1));
80 sized_array_free(void *p
)
82 struct sized_array
*sa
;
87 sa
= ((struct sized_array
*)p
)-1;
90 assert(memcmp(sa
->cookie
, COOKIE
, sizeof (sa
->cookie
)) == 0);
97 sized_array_n(void *p
)
99 struct sized_array
*sa
;
101 sa
= ((struct sized_array
*)p
)-1;
104 assert(memcmp(sa
->cookie
, COOKIE
, sizeof (sa
->cookie
)) == 0);
111 sized_array_sz(void *p
)
113 struct sized_array
*sa
;
115 sa
= ((struct sized_array
*)p
)-1;
118 assert(memcmp(sa
->cookie
, COOKIE
, sizeof (sa
->cookie
)) == 0);