chiark / gitweb /
src/method-impl.lisp: Initialize `suppliedp' flags properly.
[sod] / lib / sod-hosted.c
CommitLineData
a42893dd
MW
1/* -*-c-*-
2 *
3 * Runtime support for SOD requiring hosted implementation
4 *
5 * (c) 2015 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Sensible Object Design, an object system for C.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28/*----- Header files ------------------------------------------------------*/
29
30#include <stdlib.h>
31
32#include "sod.h"
33
34/*----- Main code ---------------------------------------------------------*/
35
36/* --- @sod_make@, @sod_makev@ --- *
37 *
38 * Arguments: @const SodClass *cls@ = class object for new instance
39 * @va_list ap, ...@ = initialization keyword arguments
40 *
41 * Returns: Pointer to the newly-allocated initialized instance, or null.
42 *
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.
46 *
47 * This function will allocate the storage using @malloc@, and
48 * then initialize it as for @sod_init@.
49 *
50 * It's usually convenient to use the macro @SOD_MAKE@ rather
51 * than calling @sod_make@ directly.
52 *
53 * (This function is not available in freestanding environments
54 * lacking @malloc@ and @free@.)
55 */
56
57void *sod_make(const SodClass *cls, ...)
58{
59 void *p;
60 va_list ap;
61
62 va_start(ap, cls);
63 p = sod_makev(cls, ap);
64 va_end(ap);
65 return (p);
66}
67
68void *sod_makev(const SodClass *cls, va_list ap)
69{
70 void *p;
71
72 if ((p = malloc(cls->cls.initsz)) == 0) return (0);
73 return (sod_initv(cls, p, ap));
74}
75
76/* --- @sod_destroy@ --- *
77 *
78 * Arguments: @void *p@ = pointer to an instance to be torn down, or null
79 *
80 * Returns: Zero if the object was freed; nonzero if it refused for some
81 * reason.
82 *
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.
87 *
88 * If @p@ is null, then this function does nothing except
89 * returns zero.
90 *
91 * (This function is not available in freestanding environments
92 * lacking @malloc@ and @free@.)
93 */
94
95int sod_destroy(void *p)
96{
97 int rc;
98
99 if (!p) return (0);
100 rc = sod_teardown(p);
101 if (!rc) free(p);
102 return (rc);
103}
104
105/*----- That's all, folks -------------------------------------------------*/