diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c index 03770af9e5cda9a6c01c177859e2f073b14b2a03..248ed3945becc7ff7d38e0b9f52250782619f65a 100644 --- a/tools/perf/util/thread-stack.c +++ b/tools/perf/util/thread-stack.c @@ -114,20 +114,25 @@ static int thread_stack__init(struct thread_stack *ts, struct thread *thread, static struct thread_stack *thread_stack__new(struct thread *thread, struct call_return_processor *crp) { - struct thread_stack *ts; - - ts = zalloc(sizeof(struct thread_stack)); - if (!ts) - return NULL; - - ts->arr_sz = 1; - - if (thread_stack__init(ts, thread, crp)) { - free(ts); - return NULL; + struct thread_stack *ts = thread->ts, *new_ts; + unsigned int old_sz = ts ? ts->arr_sz : 0; + unsigned int new_sz = 1; + + if (!ts || new_sz > old_sz) { + new_ts = calloc(new_sz, sizeof(*ts)); + if (!new_ts) + return NULL; + if (ts) + memcpy(new_ts, ts, old_sz * sizeof(*ts)); + new_ts->arr_sz = new_sz; + zfree(&thread->ts); + thread->ts = new_ts; + ts = new_ts; } - thread->ts = ts; + if (!ts->stack && + thread_stack__init(ts, thread, crp)) + return NULL; return ts; }