5432N/A/*
5432N/A * CDDL HEADER START
5432N/A *
5432N/A * The contents of this file are subject to the terms of the
5432N/A * Common Development and Distribution License (the "License").
5432N/A * You may not use this file except in compliance with the License.
5432N/A *
5432N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5432N/A * or http://www.opensolaris.org/os/licensing.
5432N/A * See the License for the specific language governing permissions
5432N/A * and limitations under the License.
5432N/A *
5432N/A * When distributing Covered Code, include this CDDL HEADER in each
5432N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5432N/A * If applicable, add the following below this CDDL HEADER, with the
5432N/A * fields enclosed by brackets "[]" replaced with your own identifying
5432N/A * information: Portions Copyright [yyyy] [name of copyright owner]
5432N/A *
5432N/A * CDDL HEADER END
5432N/A *
5432N/A * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
5432N/A */
5432N/A
5432N/A/* A Solaris implementation of the Itanium C++ ABI __cxa_atexit
5432N/A * and __cxa_finalize functions.
5432N/A */
5432N/A
5432N/A#include <stdlib.h>
5432N/A#include <pthread.h>
5432N/A
5432N/A#include "cxa_finalize.h"
5432N/A
5432N/A#define __CXA_FUNC_CALLBACK 0
5432N/A#define __CXA_FUNC_DONE 1
5432N/A#define __CXA_ATEXIT_CHUNK 64
5432N/A
5432N/A#define __DSO_TYPE_DESTRUCTOR 1
5432N/A#define __DSO_TYPE_DLCLOSE 2
5432N/A
5432N/A#define __DSO_SCOPE_LOCAL 4
5432N/A#define __DSO_SCOPE_GLOBAL 8
5432N/A
5432N/A#ifdef __cplusplus
5432N/Aextern "C" {
5432N/A#endif
5432N/A
5432N/Atypedef struct __cxxabi_atexit {
5432N/A void (*cxa_func)(void*);
5432N/A uint32_t type;
5432N/A uint32_t dso_type;
5432N/A uint32_t dso_scope;
5432N/A void* arg;
5432N/A void* dso;
5432N/A} cxxabi_atexit;
5432N/A
5432N/Atypedef struct __cxxabi_atexit_list {
5432N/A uint32_t size;
5432N/A uint32_t nelem;
5432N/A cxxabi_atexit* list;
5432N/A} cxxabi_atexit_list;
5432N/A
5432N/A__attribute__((__visibility__("hidden")))
5432N/Aextern void* __dso_handle;
5432N/A
5432N/Astatic cxxabi_atexit_list _list;
5432N/Astatic pthread_mutex_t _mtx = PTHREAD_MUTEX_INITIALIZER;
5432N/Astatic uint32_t _registered = 0;
5432N/Astatic uint32_t _initialized = 0;
5432N/A
5432N/Astatic int __register_with_atexit(const cxxabi_atexit* cxa);
5432N/Astatic int __init_cxxabi_list(cxxabi_atexit** list);
5432N/Astatic int __resize_cxxabi_list(const cxxabi_atexit* oldlist, uint32_t nelem,
5432N/A cxxabi_atexit** newlist, uint32_t newsize);
5432N/Avoid __cxa_atexit_callback(void);
5432N/Astatic void __attribute__((destructor)) __do_cleanup(void);
5432N/A
5432N/Aint __init_cxxabi_list(cxxabi_atexit** list) {
5432N/A cxxabi_atexit* __p =
5432N/A (cxxabi_atexit *) malloc (__CXA_ATEXIT_CHUNK * sizeof(cxxabi_atexit));
5432N/A
5432N/A if (__p == NULL)
5432N/A return -1;
5432N/A
5432N/A *list = __p;
5432N/A return 0;
5432N/A}
5432N/A
5432N/Aint __resize_cxxabi_list(const cxxabi_atexit* oldlist, uint32_t nelem,
5432N/A cxxabi_atexit** newlist, uint32_t newsize) {
5432N/A register cxxabi_atexit* __p =
5432N/A (cxxabi_atexit *) malloc (newsize * sizeof(cxxabi_atexit));
5432N/A
5432N/A if (__p == NULL)
5432N/A return -1;
5432N/A
5432N/A const cxxabi_atexit* __l = oldlist;
5432N/A
5432N/A for (uint32_t i = 0; i < nelem; ++i)
5432N/A *(__p + i) = *(__l + i);
5432N/A
5432N/A *newlist = __p;
5432N/A
5432N/A return 0;
5432N/A}
5432N/A
5432N/Aint __register_with_atexit(const cxxabi_atexit* cxa) {
5432N/A int ret = 0;
5432N/A
5432N/A if (cxa == NULL)
5432N/A return -1;
5432N/A
5432N/A pthread_mutex_lock(&_mtx);
5432N/A
5432N/A if (_initialized == 0) {
5432N/A _list.nelem = 0;
5432N/A
5432N/A ret = __init_cxxabi_list(&_list.list);
5432N/A
5432N/A if (ret == -1) {
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A return ret;
5432N/A }
5432N/A
5432N/A _list.size = __CXA_ATEXIT_CHUNK;
5432N/A _initialized = 1;
5432N/A }
5432N/A
5432N/A if (_list.nelem == _list.size) {
5432N/A uint32_t newsize = _list.size * 2;
5432N/A cxxabi_atexit* __p;
5432N/A
5432N/A ret = __resize_cxxabi_list(_list.list, _list.nelem, &__p, newsize);
5432N/A
5432N/A if (ret == -1) {
5432N/A free (_list.list);
5432N/A _list.list = NULL;
5432N/A _list.size = 0;
5432N/A _list.nelem = 0;
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A return ret;
5432N/A }
5432N/A
5432N/A _list.list = __p;
5432N/A _list.size = newsize;
5432N/A }
5432N/A
5432N/A if (_registered == 0) {
5432N/A ret = atexit(__cxa_atexit_callback);
5432N/A _registered = (ret == 0);
5432N/A }
5432N/A
5432N/A if (_registered != 1) {
5432N/A free (_list.list);
5432N/A _list.list = NULL;
5432N/A _list.size = 0;
5432N/A _list.nelem = 0;
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A return ret;
5432N/A }
5432N/A
5432N/A register cxxabi_atexit* __p = _list.list + _list.nelem;
5432N/A *__p = *cxa;
5432N/A ++_list.nelem;
5432N/A
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A return ret;
5432N/A}
5432N/A
5432N/Avoid __cxa_atexit_callback(void) {
5432N/A cxxabi_atexit* __e = _list.list + (_list.nelem - 1);
5432N/A cxxabi_atexit* __p = __e;
5432N/A
5432N/A if (__p == NULL)
5432N/A return;
5432N/A
5432N/A /* destroy local scope objects first */
5432N/A for (int i = _list.nelem; i > 0; --i) {
5432N/A if ((__p->type == __CXA_FUNC_CALLBACK) &&
5432N/A (__p->dso_scope == __DSO_SCOPE_LOCAL)) {
5432N/A __p->type = __CXA_FUNC_DONE;
5432N/A __p->cxa_func(__p->arg);
5432N/A }
5432N/A --__p;
5432N/A }
5432N/A
5432N/A __p = __e;
5432N/A
5432N/A /* destroy global scope objects last */
5432N/A for (int i = _list.nelem; i > 0; --i) {
5432N/A if ((__p->type == __CXA_FUNC_CALLBACK) &&
5432N/A (__p->dso_scope == __DSO_SCOPE_GLOBAL)) {
5432N/A __p->type = __CXA_FUNC_DONE;
5432N/A __p->cxa_func(__p->arg);
5432N/A }
5432N/A --__p;
5432N/A }
5432N/A}
5432N/A
5432N/Aint __cxa_atexit(void (*destructor)(void*), void* arg, void* dso) {
5432N/A cxxabi_atexit cxa;
5432N/A
5432N/A cxa.type = __CXA_FUNC_CALLBACK;
5432N/A
5432N/A /* get dso_type from dso object itself */
5432N/A cxa.dso_type = __DSO_TYPE_DESTRUCTOR;
5432N/A
5432N/A /* get dso scope from dso object */
5432N/A cxa.dso_scope = __DSO_SCOPE_GLOBAL;
5432N/A cxa.cxa_func = destructor;
5432N/A cxa.arg = arg;
5432N/A cxa.dso = dso;
5432N/A
5432N/A return __register_with_atexit(&cxa);
5432N/A}
5432N/A
5432N/Aint __cxa_finalize(void* dso) {
5432N/A uint32_t i;
5432N/A pthread_mutex_lock(&_mtx);
5432N/A
5432N/A if (_initialized == 0) {
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A return -1;
5432N/A }
5432N/A
5432N/A cxxabi_atexit* __p = _list.list;
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A
5432N/A if (__p == NULL)
5432N/A return -1;
5432N/A
5432N/A if (dso == NULL) {
5432N/A __p = _list.list + (_list.nelem - 1);
5432N/A
5432N/A pthread_mutex_lock(&_mtx);
5432N/A
5432N/A for (i = _list.nelem; i > 0; --i) {
5432N/A if (__p->type == __CXA_FUNC_DONE) {
5432N/A --__p;
5432N/A continue;
5432N/A }
5432N/A
5432N/A uint32_t __type = __p->type;
5432N/A __p->type = __CXA_FUNC_DONE;
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A
5432N/A if (__type == __CXA_FUNC_CALLBACK)
5432N/A __p->cxa_func(__p->arg);
5432N/A
5432N/A --__p;
5432N/A pthread_mutex_lock(&_mtx);
5432N/A }
5432N/A
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A } else {
5432N/A i = 0;
5432N/A do {
5432N/A if (__p->dso == dso) {
5432N/A pthread_mutex_lock(&_mtx);
5432N/A uint32_t __type = __p->type;
5432N/A __p->type = __CXA_FUNC_DONE;
5432N/A pthread_mutex_unlock(&_mtx);
5432N/A
5432N/A if (__type == __CXA_FUNC_CALLBACK)
5432N/A __p->cxa_func(__p->arg);
5432N/A }
5432N/A ++__p; ++i;
5432N/A } while (i < _list.nelem);
5432N/A }
5432N/A
5432N/A return 0;
5432N/A}
5432N/A
5432N/Avoid __attribute__((destructor)) __do_cleanup(void) {
5432N/A __cxa_finalize(__dso_handle);
5432N/A}
5432N/A
5432N/A#ifdef __cplusplus
5432N/A}
5432N/A#endif
5432N/A