3 * Runtime support for SOD requiring hosted implementation
5 * (c) 2015 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Sensible Object Design, an object system for C.
12 * SOD is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * SOD is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with SOD; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 /*----- Main code ---------------------------------------------------------*/
36 /* --- @sod_make@, @sod_makev@ --- *
38 * Arguments: @const SodClass *cls@ = class object for new instance
39 * @va_list ap, ...@ = initialization keyword arguments
41 * Returns: Pointer to the newly-allocated initialized instance, or null.
43 * Use: Allocates storage for a new instance, initializes it, and
44 * returns a pointer to it. If allocation fails, a null pointer
45 * is returned instead.
47 * This function will allocate the storage using @malloc@, and
48 * then initialize it as for @sod_init@.
50 * It's usually convenient to use the macro @SOD_MAKE@ rather
51 * than calling @sod_make@ directly.
53 * (This function is not available in freestanding environments
54 * lacking @malloc@ and @free@.)
57 void *sod_make(const SodClass *cls, ...)
63 p = sod_makev(cls, ap);
68 void *sod_makev(const SodClass *cls, va_list ap)
72 if ((p = malloc(cls->cls.initsz)) == 0) return (0);
73 return (sod_initv(cls, p, ap));
76 /* --- @sod_destroy@ --- *
78 * Arguments: @void *p@ = pointer to an instance to be torn down, or null
80 * Returns: Zero if the object was freed; nonzero if it refused for some
83 * Use: Invokes the instance's `teardown' method to release any held
84 * resources, and then calls @free@ to release the instance's
85 * storage. See @sod_teardown@ for details, especially
86 * regarding the return value's meaning.
88 * If @p@ is null, then this function does nothing except
91 * (This function is not available in freestanding environments
92 * lacking @malloc@ and @free@.)
95 int sod_destroy(void *p)
100 rc = sod_teardown(p);
105 /*----- That's all, folks -------------------------------------------------*/