mirror of
https://github.com/ziglang/zig.git
synced 2026-01-04 20:43:19 +00:00
Deleted 16,000+ lines of c++ code, including:
* an implementation of blake hashing
* the cache hash system
* compiler.cpp
* all the linking code, and everything having to do with building
glibc, musl, and mingw-w64
* much of the stage1 compiler internals got slimmed down since it
now assumes it is always outputting an object file.
More stuff:
* stage1 is now built with a different strategy: we have a tiny
zig0.cpp which is a slimmed down version of what stage1 main.cpp used
to be. Its only purpose is to build stage2 zig code into an object
file, which is then linked by the host build system (cmake) into
stage1. zig0.cpp uses the same C API that stage2 now has access to,
so that stage2 zig code can call into stage1 c++ code.
- stage1.h is
- stage2.h is
- stage1.zig is the main entry point for the Zig/C++
hybrid compiler. It has the functions exported from Zig, called
in C++, and bindings for the functions exported from C++, called
from Zig.
* removed the memory profiling instrumentation from stage1.
Abandon ship!
* Re-added the sections to the README about how to build stage2 and
stage3.
* stage2 now knows as a comptime boolean whether it is being compiled
as part of stage1 or as stage2.
- TODO use this flag to call into stage1 for compiling zig code.
* introduce -fdll-export-fns and -fno-dll-export-fns and clarify
its relationship to link_mode (static/dynamic)
* implement depending on LLVM to detect native target cpu features when
LLVM extensions are enabled and zig lacks CPU feature detection for
that target architecture.
* C importing is broken, will need some stage2 support to function
again.
141 lines
3.9 KiB
C++
141 lines
3.9 KiB
C++
/*
|
|
* Copyright (c) 2020 Andrew Kelley
|
|
*
|
|
* This file is part of zig, which is MIT licensed.
|
|
* See http://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#ifndef ZIG_MEM_HPP
|
|
#define ZIG_MEM_HPP
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "config.h"
|
|
#include "util_base.hpp"
|
|
#include "mem_type_info.hpp"
|
|
|
|
//
|
|
// -- Memory Allocation General Notes --
|
|
//
|
|
// `heap::c_allocator` is the preferred general allocator.
|
|
//
|
|
// `heap::bootstrap_allocator` is an implementation detail for use
|
|
// by allocators themselves when incidental heap may be required for
|
|
// profiling and statistics. It breaks the infinite recursion cycle.
|
|
//
|
|
// `mem::os` contains a raw wrapper for system malloc API used in
|
|
// preference to calling ::{malloc, free, calloc, realloc} directly.
|
|
// This isolates usage and helps with audits:
|
|
//
|
|
// mem::os::malloc
|
|
// mem::os::free
|
|
// mem::os::calloc
|
|
// mem::os::realloc
|
|
//
|
|
namespace mem {
|
|
|
|
// initialize mem module before any use
|
|
void init();
|
|
|
|
// deinitialize mem module to free memory and print report
|
|
void deinit();
|
|
|
|
// isolate system/libc allocators
|
|
namespace os {
|
|
|
|
ATTRIBUTE_RETURNS_NOALIAS
|
|
inline void *malloc(size_t size) {
|
|
#ifndef NDEBUG
|
|
// make behavior when size == 0 portable
|
|
if (size == 0)
|
|
return nullptr;
|
|
#endif
|
|
auto ptr = ::malloc(size);
|
|
if (ptr == nullptr)
|
|
zig_panic("allocation failed");
|
|
return ptr;
|
|
}
|
|
|
|
inline void free(void *ptr) {
|
|
::free(ptr);
|
|
}
|
|
|
|
ATTRIBUTE_RETURNS_NOALIAS
|
|
inline void *calloc(size_t count, size_t size) {
|
|
#ifndef NDEBUG
|
|
// make behavior when size == 0 portable
|
|
if (count == 0 || size == 0)
|
|
return nullptr;
|
|
#endif
|
|
auto ptr = ::calloc(count, size);
|
|
if (ptr == nullptr)
|
|
zig_panic("allocation failed");
|
|
return ptr;
|
|
}
|
|
|
|
inline void *realloc(void *old_ptr, size_t size) {
|
|
#ifndef NDEBUG
|
|
// make behavior when size == 0 portable
|
|
if (old_ptr == nullptr && size == 0)
|
|
return nullptr;
|
|
#endif
|
|
auto ptr = ::realloc(old_ptr, size);
|
|
if (ptr == nullptr)
|
|
zig_panic("allocation failed");
|
|
return ptr;
|
|
}
|
|
|
|
} // namespace os
|
|
|
|
struct Allocator {
|
|
virtual void destruct(Allocator *allocator) = 0;
|
|
|
|
template <typename T> ATTRIBUTE_RETURNS_NOALIAS
|
|
T *allocate(size_t count) {
|
|
return reinterpret_cast<T *>(this->internal_allocate(TypeInfo::make<T>(), count));
|
|
}
|
|
|
|
template <typename T> ATTRIBUTE_RETURNS_NOALIAS
|
|
T *allocate_nonzero(size_t count) {
|
|
return reinterpret_cast<T *>(this->internal_allocate_nonzero(TypeInfo::make<T>(), count));
|
|
}
|
|
|
|
template <typename T>
|
|
T *reallocate(T *old_ptr, size_t old_count, size_t new_count) {
|
|
return reinterpret_cast<T *>(this->internal_reallocate(TypeInfo::make<T>(), old_ptr, old_count, new_count));
|
|
}
|
|
|
|
template <typename T>
|
|
T *reallocate_nonzero(T *old_ptr, size_t old_count, size_t new_count) {
|
|
return reinterpret_cast<T *>(this->internal_reallocate_nonzero(TypeInfo::make<T>(), old_ptr, old_count, new_count));
|
|
}
|
|
|
|
template<typename T>
|
|
void deallocate(T *ptr, size_t count) {
|
|
this->internal_deallocate(TypeInfo::make<T>(), ptr, count);
|
|
}
|
|
|
|
template<typename T>
|
|
T *create() {
|
|
return reinterpret_cast<T *>(this->internal_allocate(TypeInfo::make<T>(), 1));
|
|
}
|
|
|
|
template<typename T>
|
|
void destroy(T *ptr) {
|
|
this->internal_deallocate(TypeInfo::make<T>(), ptr, 1);
|
|
}
|
|
|
|
protected:
|
|
ATTRIBUTE_RETURNS_NOALIAS virtual void *internal_allocate(const TypeInfo &info, size_t count) = 0;
|
|
ATTRIBUTE_RETURNS_NOALIAS virtual void *internal_allocate_nonzero(const TypeInfo &info, size_t count) = 0;
|
|
virtual void *internal_reallocate(const TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) = 0;
|
|
virtual void *internal_reallocate_nonzero(const TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) = 0;
|
|
virtual void internal_deallocate(const TypeInfo &info, void *ptr, size_t count) = 0;
|
|
};
|
|
|
|
} // namespace mem
|
|
|
|
#endif
|