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 ------------------------------------------------------*/
35 /*----- Preliminary macros ------------------------------------------------*/
37 #if __STDC_VERSION__ >= 199901
39 # define PRINT_SZ(x) (x)
42 # define PRINT_SZ(x) ((unsigned long)(x))
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @sod__chksz_fail@ --- *
49 * Arguments: @const SodClass *cls@ = class we were trying to instantiate
50 * @size_t sz@ = size allocated
54 * Use: Reports instantiation failure caused by a mismatch between
55 * the size allocated (@sz@) and the size required for an
56 * instance of class @cls@.
59 SOD__NORETURN void sod__chksz_fail(const SodClass *cls, size_t sz)
61 fprintf(stderr, "INTERNAL ERROR: size mismatch for class `%s': "
62 "%"PRIuSZ" allocated but %"PRIuSZ" required",
63 cls->cls.name, PRINT_SZ(sz), PRINT_SZ(cls->cls.initsz));
67 /* --- @sod_make@, @sod_makev@ --- *
69 * Arguments: @const SodClass *cls@ = class object for new instance
70 * @va_list ap, ...@ = initialization keyword arguments
72 * Returns: Pointer to the newly-allocated initialized instance, or null.
74 * Use: Allocates storage for a new instance, initializes it, and
75 * returns a pointer to it. If allocation fails, a null pointer
76 * is returned instead.
78 * This function will allocate the storage using @malloc@, and
79 * then initialize it as for @sod_init@.
81 * It's usually convenient to use the macro @SOD_MAKE@ rather
82 * than calling @sod_make@ directly.
84 * (This function is not available in freestanding environments
85 * lacking @malloc@ and @free@.)
88 void *sod_make(const SodClass *cls, ...)
94 p = sod_makev(cls, ap);
99 void *sod_makev(const SodClass *cls, va_list ap)
103 p = malloc(cls->cls.initsz); if (!p) return (0);
104 return (sod_initv(cls, p, ap));
107 /* --- @sod_destroy@ --- *
109 * Arguments: @void *p@ = pointer to an instance to be torn down, or null
111 * Returns: Zero if the object was freed; nonzero if it refused for some
114 * Use: Invokes the instance's `teardown' method to release any held
115 * resources, and then calls @free@ to release the instance's
116 * storage. See @sod_teardown@ for details, especially
117 * regarding the return value's meaning.
119 * If @p@ is null, then this function does nothing except
122 * (This function is not available in freestanding environments
123 * lacking @malloc@ and @free@.)
126 int sod_destroy(void *p)
131 rc = sod_teardown(p);
136 /*----- That's all, folks -------------------------------------------------*/