Files
swift-mirror/runtime/Alloc.cpp
Eli Friedman 283ef8e455 Build swift.swift as part of the swift runtime. The CMake changes here aren't complete, but Doug said he would fix that.
This should be enough to allow statically compiling swift applications.  As followups, I'm going to additionally start installing swift.swift to a common location, and start  making REPL/script mode aware that these functions are available, so they don't bother to IRGen them.

I'm leaving around some files in runtime/ which aren't necessary with this commit; I'll delete them in a separate commit.



Swift SVN r1725
2012-05-03 22:31:23 +00:00

71 lines
1.8 KiB
C++

//===--- Alloc.cpp - Swift Language ABI Allocation Support ----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Allocation ABI Shims While the Language is Bootstrapped
//
//===----------------------------------------------------------------------===//
#include "Alloc.h"
#include <llvm/Support/MathExtras.h>
#include <cstdlib>
#include <unistd.h>
struct SwiftHeapObject *
swift_alloc(struct SwiftHeapMetadata *metadata,
size_t requiredSize,
size_t requiredAlignment)
{
struct SwiftHeapObject *object;
for (;;) {
object = reinterpret_cast<struct SwiftHeapObject *>(
calloc(1, llvm::RoundUpToAlignment(requiredSize, requiredAlignment)));
if (object) {
break;
}
sleep(1); // XXX FIXME -- Enqueue this thread and resume after free()
}
object->metadata = metadata;
object->runtimePrivateData = 1;
return reinterpret_cast<struct SwiftHeapObject *>(object);
}
struct SwiftHeapObject *
swift_retain(struct SwiftHeapObject *object)
{
if (!object) {
return NULL;
}
++object->runtimePrivateData;
return object;
}
void
swift_release(struct SwiftHeapObject *object)
{
if (!object) {
return;
}
if (--object->runtimePrivateData > 0) {
return;
}
size_t allocSize = object->metadata->destroy(object);
if (allocSize) {
swift_dealloc(object, allocSize);
}
}
void
swift_dealloc(struct SwiftHeapObject *object, size_t allocatedSize)
{
free(object);
}