blaise/rtl/src/main/c/blaise_arc_class.c
Graeme Geldenhuys 5894411a35 chore(license): relicense from BSD-3-Clause to Apache 2.0 + Runtime Library Exception
Adopts the Swift/LLVM model: Apache License 2.0 with the Runtime Library
Exception text used verbatim by the Swift project. SPDX identifier:
Apache-2.0 WITH Swift-exception.

Apache 2.0 brings an explicit patent grant and patent-retaliation clause
that BSD-3 lacks. The Runtime Library Exception ensures binaries
produced by the Blaise compiler do not inherit attribution obligations
from the linked-in RTL.

* LICENSE — full Apache 2.0 + RLE text (replaces the deleted LICENCE).
* NOTICE — project header plus QBE attribution (MIT, vendored).
* docs/language-rationale.adoc — new Project Governance section
  capturing the decision, alternatives considered, and rationale.
* SPDX headers updated across all .pas, .pp, .inc, .c source files,
  build scripts, PasBuild plugins, and the Makefile.
* project.xml license field updated.
* tools/migrate_full.py HEADER template updated so generated
  self-hosting source carries the new licence.
2026-05-03 19:45:29 +01:00

58 lines
1.9 KiB
C

/*
* Blaise — An Object Pascal Compiler
* Copyright (c) 2026 Graeme Geldenhuys
* SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
* Licensed under the Apache License v2.0 with Runtime Library Exception.
* See LICENSE file in the project root for full license terms.
*
* Blaise RTL — class ARC (function-pointer-dependent subset)
*
* _ClassAlloc and _ClassRelease remain in C because they store and call a
* function pointer (the field-cleanup hook emitted by the compiler as
* $_FieldCleanup_TypeName). All other ARC functions live in blaise_arc.pas.
*
* Class instance layout:
* +--[4 bytes]--+--[4 bytes]--+--[8 bytes]--+--[user fields...]--+
* | RefCount | (padding) | cleanup ptr | ... |
* +-------------+-------------+-------------+--------------------+
* ^--- user pointer
*/
#include <stdint.h>
#include <stdlib.h>
typedef void (*BlaiseFieldCleanupFn)(void*);
typedef struct {
int32_t refcnt;
int32_t _pad;
BlaiseFieldCleanupFn cleanup;
} BlaiseObjHdr;
#define CLASS_HDR_SIZE (sizeof(BlaiseObjHdr))
static inline BlaiseObjHdr* obj_hdr(void* user_ptr) {
return (BlaiseObjHdr*)((char*)user_ptr - CLASS_HDR_SIZE);
}
void* _ClassAlloc(size_t size, BlaiseFieldCleanupFn cleanup) {
char* base = (char*)calloc(1, size + CLASS_HDR_SIZE);
if (!base) return NULL;
BlaiseObjHdr* h = (BlaiseObjHdr*)base;
h->cleanup = cleanup;
return base + CLASS_HDR_SIZE;
}
/* Forward declaration — implemented in blaise_weak.c. */
extern void _WeakZeroSlots(void* target);
void _ClassRelease(void* user_ptr) {
if (!user_ptr) return;
BlaiseObjHdr* h = obj_hdr(user_ptr);
if (--h->refcnt == 0) {
_WeakZeroSlots(user_ptr);
if (h->cleanup) h->cleanup(user_ptr);
free((char*)user_ptr - CLASS_HDR_SIZE);
}
}