Skip to content

Commit

Permalink
replace malloc with operator new
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiguchi-nagisa committed Apr 20, 2024
1 parent cb736d1 commit c1fcf77
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 38 deletions.
6 changes: 1 addition & 5 deletions src/candidates.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ class CandidateObject : public ObjectWithRtti<ObjectKind::Candidate> {
assert(description.size() <= MAX_SIZE);
assert(candidate.size() + 1 <= MAX_SIZE - description.size());
const unsigned int allocSize = candidate.size() + description.size() + 1;
void *ptr = malloc(sizeof(CandidateObject) + sizeof(char) * allocSize);
void *ptr = operator new(sizeof(CandidateObject) + sizeof(char) * allocSize);
auto *obj = new (ptr) CandidateObject(candidate, description);
return ObjPtr<CandidateObject>(obj);
}

static void operator delete(void *ptr) noexcept { // NOLINT
free(ptr);
}

unsigned int candidateSize() const { return this->canSize; }

unsigned int descriptionSize() const { return this->descSize; }
Expand Down
6 changes: 1 addition & 5 deletions src/job.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class JobObject : public ObjectWithRtti<ObjectKind::Job> {
static ObjPtr<JobObject> create(unsigned int size, const Proc *procs, bool saveStdin,
ObjPtr<UnixFdObject> inObj, ObjPtr<UnixFdObject> outObj,
Value &&desc) {
void *ptr = malloc(sizeof(JobObject) + sizeof(Proc) * size);
void *ptr = operator new(sizeof(JobObject) + sizeof(Proc) * size);
auto *entry = new (ptr)
JobObject(size, procs, saveStdin, std::move(inObj), std::move(outObj), std::move(desc));
return ObjPtr<JobObject>(entry);
Expand All @@ -266,10 +266,6 @@ class JobObject : public ObjectWithRtti<ObjectKind::Job> {
return create(1, procs, false, std::move(inObj), std::move(outObj), std::move(desc));
}

static void operator delete(void *ptr) noexcept { // NOLINT
free(ptr);
}

unsigned int getProcSize() const { return this->procSize; }

State state() const { return static_cast<State>(STATE_MASK & this->meta); }
Expand Down
14 changes: 2 additions & 12 deletions src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ class BaseObject : public ObjectWithRtti<ObjectKind::Base> {

public:
static BaseObject *create(const DSType &type, unsigned int size) {
void *ptr = malloc(sizeof(BaseObject) + sizeof(RawValue) * size);
void *ptr = operator new(sizeof(BaseObject) + sizeof(RawValue) * size);
return new (ptr) BaseObject(type, size);
}

Expand All @@ -960,11 +960,6 @@ class BaseObject : public ObjectWithRtti<ObjectKind::Base> {
return static_cast<const Value &>(this->fields[index]);
}

static void operator delete(void *ptr) noexcept {
// NOLINT
free(ptr);
}

unsigned int getFieldSize() const { return this->fieldSize; }
};

Expand Down Expand Up @@ -1286,7 +1281,7 @@ class ClosureObject : public ObjectWithRtti<ObjectKind::Closure> {

public:
static ClosureObject *create(ObjPtr<FuncObject> func, unsigned int size, const Value *values) {
void *ptr = malloc(sizeof(ClosureObject) + sizeof(RawValue) * size);
void *ptr = operator new(sizeof(ClosureObject) + sizeof(RawValue) * size);
auto *closure = new (ptr) ClosureObject(std::move(func), size);
for (unsigned int i = 0; i < size; i++) {
(*closure)[i] = values[i];
Expand All @@ -1303,11 +1298,6 @@ class ClosureObject : public ObjectWithRtti<ObjectKind::Closure> {
const Value &operator[](unsigned int index) const {
return static_cast<const Value &>(this->upvars[index]);
}

static void operator delete(void *ptr) noexcept {
// NOLINT
free(ptr);
}
};

template <typename... Arg>
Expand Down
2 changes: 1 addition & 1 deletion src/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ std::unique_ptr<MethodHandle> MethodHandle::create(const DSType &recv, unsigned
PackedParamNames &&packed, ModId modId) {
const size_t paramSize = params.size();
assert(paramSize <= SYS_LIMIT_METHOD_PARAM_NUM);
void *ptr = malloc(sizeof(MethodHandle) + sizeof(uintptr_t) * paramSize);
void *ptr = operator new(sizeof(MethodHandle) + sizeof(uintptr_t) * paramSize);
const auto &actualRet = ret ? *ret : recv;
auto *handle = new (ptr) MethodHandle(recv, index, actualRet, paramSize, modId,
ret ? HandleKind::METHOD : HandleKind::CONSTRUCTOR);
Expand Down
18 changes: 3 additions & 15 deletions src/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ class FunctionType : public DSType {
public:
static FunctionType *create(unsigned int id, StringRef ref, const DSType &superType,
const DSType &returnType, std::vector<const DSType *> &&paramTypes) {
void *ptr = malloc(sizeof(FunctionType) + sizeof(DSType *) * paramTypes.size());
void *ptr = operator new(sizeof(FunctionType) + sizeof(DSType *) * paramTypes.size());
return new (ptr) FunctionType(id, ref, superType, returnType, std::move(paramTypes));
}

Expand All @@ -513,10 +513,6 @@ class FunctionType : public DSType {
this->getParamSize() == 0 ? nullptr : &this->paramTypes[0], name, hd};
}

static void operator delete(void *ptr) noexcept { // NOLINT
free(ptr);
}

static bool classof(const DSType *type) { return type->isFuncType(); }
};

Expand Down Expand Up @@ -956,15 +952,11 @@ class FuncHandle : public Handle {
if (famSize) {
famSize++; // reserve sentinel
}
void *ptr = malloc(sizeof(FuncHandle) + sizeof(char) * famSize);
void *ptr = operator new(sizeof(FuncHandle) + sizeof(char) * famSize);
auto *handle = new (ptr) FuncHandle(funcType, index, std::move(packed), modId);
return std::unique_ptr<FuncHandle>(handle);
}

static void operator delete(void *ptr) noexcept { // NOLINT
free(ptr);
}

StringRef getPackedParamNames() const {
StringRef ref;
if (this->hasParams()) {
Expand Down Expand Up @@ -1021,14 +1013,10 @@ class MethodHandle : public Handle {

~MethodHandle();

static void operator delete(void *ptr) noexcept { // NOLINT
free(ptr);
}

static std::unique_ptr<MethodHandle>
native(const DSType &recv, unsigned int index, const DSType &ret, unsigned char paramSize,
const std::array<const DSType *, HandleInfoParamNumMax()> &paramTypes) {
void *ptr = malloc(sizeof(MethodHandle) + sizeof(uintptr_t) * paramSize);
void *ptr = operator new(sizeof(MethodHandle) + sizeof(uintptr_t) * paramSize);
auto *handle =
new (ptr) MethodHandle(recv, index, ret, paramSize, BUILTIN_MOD_ID, HandleKind::NATIVE);
for (unsigned int i = 0; i < static_cast<unsigned int>(paramSize); i++) {
Expand Down

0 comments on commit c1fcf77

Please sign in to comment.