Skip to content

Commit

Permalink
FIX set_photon_thread_stack_allocator implemention
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings committed Jul 4, 2023
1 parent 8c8f172 commit 2ae11a9
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,17 @@ namespace photon
free(ptr);
}

Delegate<void*, size_t> photon_thread_alloc(
&default_photon_thread_stack_alloc, nullptr);
Delegate<void, void*, size_t> photon_thread_dealloc(
&default_photon_thread_stack_dealloc, nullptr);
Delegate<void *, size_t> &photon_thread_alloc() {
static Delegate<void *, size_t> _photon_thread_alloc(
&default_photon_thread_stack_alloc, nullptr);
return _photon_thread_alloc;
}

Delegate<void, void *, size_t> &photon_thread_dealloc() {
static Delegate<void, void *, size_t> _photon_thread_dealloc(
&default_photon_thread_stack_dealloc, nullptr);
return _photon_thread_dealloc;
}

struct vcpu_t;
struct thread;
Expand Down Expand Up @@ -283,7 +290,7 @@ namespace photon
assert(state == states::DONE);
// `buf` and `stack_size` will always store on register
// when calling deallocating.
photon_thread_dealloc(buf, stack_size);
photon_thread_dealloc()(buf, stack_size);
}
};

Expand Down Expand Up @@ -838,7 +845,7 @@ R"(
LOG_ERROR_RETURN(ENOSYS, nullptr, "Photon not initialized in this vCPU (OS thread)");
size_t randomizer = (rand() % 32) * (1024 + 8);
stack_size = align_up(randomizer + stack_size + sizeof(thread), PAGE_SIZE);
char* ptr = (char*)photon_thread_alloc(stack_size);
char *ptr = (char *)photon_thread_alloc()(stack_size);
auto p = ptr + stack_size - sizeof(thread) - randomizer;
(uint64_t&)p &= ~63;
auto th = new (p) thread;
Expand Down Expand Up @@ -1808,6 +1815,13 @@ R"(
return --_n_vcpu;
}

void set_photon_thread_stack_allocator(
Delegate<void *, size_t> _photon_thread_alloc,
Delegate<void, void *, size_t> _photon_thread_dealloc) {
photon_thread_alloc() = _photon_thread_alloc;
photon_thread_dealloc() = _photon_thread_dealloc;
}

void* stackful_malloc(size_t size) {
return CURRENT->stackful_malloc(size);
}
Expand Down

0 comments on commit 2ae11a9

Please sign in to comment.