Skip to content

Commit fa9814b

Browse files
author
Joshua Warner
committed
remove lots of unnecessary sizeof computations with a convenient 'operator new' overload
1 parent fde7a3e commit fa9814b

File tree

7 files changed

+92
-141
lines changed

7 files changed

+92
-141
lines changed

src/allocator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,8 @@ copy(Allocator* allocator, const char* a)
5757

5858
} // namespace vm
5959

60+
inline void* operator new (size_t size, vm::Allocator* allocator) {
61+
return allocator->allocate(size);
62+
}
63+
6064
#endif//ALLOCATOR_H

src/arm.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class Context {
228228
public:
229229
Context(System* s, Allocator* a, Zone* zone):
230230
s(s), zone(zone), client(0), code(s, a, 1024), tasks(0), result(0),
231-
firstBlock(new (zone->allocate(sizeof(MyBlock))) MyBlock(this, 0)),
231+
firstBlock(new(zone) MyBlock(this, 0)),
232232
lastBlock(firstBlock), poolOffsetHead(0), poolOffsetTail(0),
233233
constantPool(0), constantPoolCount(0)
234234
{ }
@@ -346,8 +346,7 @@ class Offset: public Promise {
346346
Promise*
347347
offset(Context* c, bool forTrace = false)
348348
{
349-
return new (c->zone->allocate(sizeof(Offset)))
350-
Offset(c, c->lastBlock, c->code.length(), forTrace);
349+
return new(c->zone) Offset(c, c->lastBlock, c->code.length(), forTrace);
351350
}
352351

353352
bool
@@ -414,8 +413,7 @@ class OffsetTask: public Task {
414413
void
415414
appendOffsetTask(Context* c, Promise* promise, Promise* instructionOffset)
416415
{
417-
c->tasks = new (c->zone->allocate(sizeof(OffsetTask))) OffsetTask
418-
(c->tasks, promise, instructionOffset);
416+
c->tasks = new(c->zone) OffsetTask(c->tasks, promise, instructionOffset);
419417
}
420418

421419
inline unsigned
@@ -628,17 +626,14 @@ appendConstantPoolEntry(Context* c, Promise* constant, Promise* callOffset)
628626
if (constant->resolved()) {
629627
// make a copy, since the original might be allocated on the
630628
// stack, and we need our copy to live until assembly is complete
631-
constant = new (c->zone->allocate(sizeof(ResolvedPromise)))
632-
ResolvedPromise(constant->value());
629+
constant = new(c->zone) ResolvedPromise(constant->value());
633630
}
634631

635-
c->constantPool = new (c->zone->allocate(sizeof(ConstantPoolEntry)))
636-
ConstantPoolEntry(c, constant, c->constantPool, callOffset);
632+
c->constantPool = new(c->zone) ConstantPoolEntry(c, constant, c->constantPool, callOffset);
637633

638634
++ c->constantPoolCount;
639635

640-
PoolOffset* o = new (c->zone->allocate(sizeof(PoolOffset))) PoolOffset
641-
(c->lastBlock, c->constantPool, c->code.length() - c->lastBlock->offset);
636+
PoolOffset* o = new(c->zone) PoolOffset(c->lastBlock, c->constantPool, c->code.length() - c->lastBlock->offset);
642637

643638
if (DebugPool) {
644639
fprintf(stderr, "add pool offset %p %d to block %p\n",
@@ -657,8 +652,7 @@ void
657652
appendPoolEvent(Context* c, MyBlock* b, unsigned offset, PoolOffset* head,
658653
PoolOffset* tail)
659654
{
660-
PoolEvent* e = new (c->zone->allocate(sizeof(PoolEvent))) PoolEvent
661-
(head, tail, offset);
655+
PoolEvent* e = new(c->zone) PoolEvent(head, tail, offset);
662656

663657
if (b->poolEventTail) {
664658
b->poolEventTail->next = e;
@@ -1522,8 +1516,7 @@ branchCM(Context* c, TernaryOperation op, unsigned size,
15221516
ShiftMaskPromise*
15231517
shiftMaskPromise(Context* c, Promise* base, unsigned shift, int64_t mask)
15241518
{
1525-
return new (c->zone->allocate(sizeof(ShiftMaskPromise)))
1526-
ShiftMaskPromise(base, shift, mask);
1519+
return new(c->zone) ShiftMaskPromise(base, shift, mask);
15271520
}
15281521

15291522
void
@@ -2158,9 +2151,7 @@ class MyAssembler: public Assembler {
21582151
{
21592152
Register stack(StackRegister);
21602153
Memory stackLimit(ThreadRegister, stackLimitOffsetFromThread);
2161-
Constant handlerConstant
2162-
(new (c.zone->allocate(sizeof(ResolvedPromise)))
2163-
ResolvedPromise(handler));
2154+
Constant handlerConstant(new(c.zone) ResolvedPromise(handler));
21642155
branchRM(&c, JumpIfGreaterOrEqual, TargetBytesPerWord, &stack, &stackLimit,
21652156
&handlerConstant);
21662157
}
@@ -2465,8 +2456,7 @@ class MyAssembler: public Assembler {
24652456
MyBlock* b = c.lastBlock;
24662457
b->size = c.code.length() - b->offset;
24672458
if (startNew) {
2468-
c.lastBlock = new (c.zone->allocate(sizeof(MyBlock)))
2469-
MyBlock(&c, c.code.length());
2459+
c.lastBlock = new (c.zone) MyBlock(&c, c.code.length());
24702460
} else {
24712461
c.lastBlock = 0;
24722462
}
@@ -2537,8 +2527,7 @@ Assembler*
25372527
makeAssembler(System* system, Allocator* allocator, Zone* zone,
25382528
Assembler::Architecture* architecture)
25392529
{
2540-
return new (zone->allocate(sizeof(MyAssembler)))
2541-
MyAssembler(system, allocator, zone,
2530+
return new(zone) MyAssembler(system, allocator, zone,
25422531
static_cast<MyArchitecture*>(architecture));
25432532
}
25442533

src/compile.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,8 +1389,7 @@ class Frame {
13891389
Compiler::Operand* append(object o) {
13901390
BootContext* bc = context->bootContext;
13911391
if (bc) {
1392-
Promise* p = new (bc->zone->allocate(sizeof(ListenPromise)))
1393-
ListenPromise(t->m->system, bc->zone);
1392+
Promise* p = new (bc->zone) ListenPromise(t->m->system, bc->zone);
13941393

13951394
PROTECT(t, o);
13961395
object pointer = makePointer(t, p);
@@ -1408,9 +1407,7 @@ class Frame {
14081407
}
14091408
}
14101409

1411-
context->objectPool = new
1412-
(context->zone.allocate(sizeof(PoolElement)))
1413-
PoolElement(t, o, context->objectPool);
1410+
context->objectPool = new(&context->zone) PoolElement(t, o, context->objectPool);
14141411

14151412
++ context->objectPoolCount;
14161413

@@ -1615,8 +1612,7 @@ class Frame {
16151612
Promise* addressPromise(Promise* p) {
16161613
BootContext* bc = context->bootContext;
16171614
if (bc) {
1618-
bc->addresses = new (bc->zone->allocate(sizeof(DelayedPromise)))
1619-
DelayedPromise(t->m->system, bc->zone, p, bc->addresses);
1615+
bc->addresses = new(bc->zone) DelayedPromise(t->m->system, bc->zone, p, bc->addresses);
16201616
return bc->addresses;
16211617
} else {
16221618
return p;
@@ -1633,7 +1629,7 @@ class Frame {
16331629
(TargetBytesPerWord, c->memory
16341630
(c->register_(t->arch->thread()), Compiler::AddressType,
16351631
TargetThreadCodeImage), c->promiseConstant
1636-
(new (context->zone.allocate(sizeof(OffsetPromise)))
1632+
(new(&context->zone)
16371633
OffsetPromise
16381634
(p, - reinterpret_cast<intptr_t>(codeAllocator(t)->base)),
16391635
Compiler::AddressType))
@@ -3417,8 +3413,7 @@ compileDirectInvoke(MyThread* t, Frame* frame, object target, bool tailCall)
34173413
and (not (TailCalls and tailCall
34183414
and (methodFlags(t, target) & ACC_NATIVE))))
34193415
{
3420-
Promise* p = new (bc->zone->allocate(sizeof(ListenPromise)))
3421-
ListenPromise(t->m->system, bc->zone);
3416+
Promise* p = new(bc->zone) ListenPromise(t->m->system, bc->zone);
34223417

34233418
PROTECT(t, target);
34243419
object pointer = makePointer(t, p);
@@ -6636,7 +6631,7 @@ calculateFrameMaps(MyThread* t, Context* context, uintptr_t* originalRoots,
66366631
}
66376632

66386633
if (path == 0) {
6639-
path = new (context->zone.allocate(sizeof(SubroutinePath)))
6634+
path = new(&context->zone)
66406635
SubroutinePath(call, subroutinePath,
66416636
makeRootTable(t, &(context->zone), context->method));
66426637
}
@@ -7061,8 +7056,7 @@ finish(MyThread* t, FixedAllocator* allocator, Context* context)
70617056
p != bc->addressSentinal;
70627057
p = p->next)
70637058
{
7064-
p->basis = new (bc->zone->allocate(sizeof(ResolvedPromise)))
7065-
ResolvedPromise(p->basis->value());
7059+
p->basis = new(bc->zone) ResolvedPromise(p->basis->value());
70667060
}
70677061
}
70687062

@@ -9724,8 +9718,7 @@ compileCall(MyThread* t, Context* c, ThunkIndex index, bool call = true)
97249718
(call ? Call : Jump, TargetBytesPerWord, RegisterOperand, &scratch);
97259719
} else {
97269720
Assembler::Constant proc
9727-
(new (c->zone.allocate(sizeof(ResolvedPromise)))
9728-
ResolvedPromise(reinterpret_cast<intptr_t>(t->thunkTable[index])));
9721+
(new(&c->zone) ResolvedPromise(reinterpret_cast<intptr_t>(t->thunkTable[index])));
97299722

97309723
a->apply
97319724
(call ? LongCall : LongJump, TargetBytesPerWord, ConstantOperand, &proc);

0 commit comments

Comments
 (0)