-
-
Notifications
You must be signed in to change notification settings - Fork 597
Fix cpumeter segfault #2025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fasterit
wants to merge
2
commits into
htop-dev:main
Choose a base branch
from
fasterit:fix-cpumeter-segfault
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−28
Open
Fix cpumeter segfault #2025
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -233,7 +233,7 @@ static void CPUMeter_display(const Object* cast, RichString* out) { | |
| #endif | ||
| } | ||
|
|
||
| static void AllCPUsMeter_getRange(const Meter* this, int* start, int* count) { | ||
| static void AllCPUsMeter_getRange(const Meter* this, unsigned int* start, unsigned int* count) { | ||
| unsigned int cpus = this->host->existingCPUs; | ||
| switch (Meter_name(this)[0]) { | ||
| default: | ||
|
|
@@ -252,47 +252,71 @@ static void AllCPUsMeter_getRange(const Meter* this, int* start, int* count) { | |
| } | ||
| } | ||
|
|
||
| static void AllCPUsMeter_updateValues(Meter* this) { | ||
| CPUMeterData* data = this->meterData; | ||
| Meter** meters = data->meters; | ||
| int start, count; | ||
| AllCPUsMeter_getRange(this, &start, &count); | ||
| for (int i = 0; i < count; i++) | ||
| Meter_updateValues(meters[i]); | ||
| } | ||
|
|
||
| static void CPUMeterCommonInit(Meter* this) { | ||
| int start, count; | ||
| unsigned int start, count, prevCount; | ||
| AllCPUsMeter_getRange(this, &start, &count); | ||
|
|
||
| CPUMeterData* data = this->meterData; | ||
| if (!data) { | ||
| data = xCalloc(1, sizeof(CPUMeterData)); | ||
| data->cpus = this->host->existingCPUs; | ||
| data->meters = count ? xCalloc(count, sizeof(Meter*)) : NULL; | ||
| data->cpus = 0; /* no meters allocated yet */ | ||
| data->meters = NULL; | ||
| this->meterData = data; | ||
| } | ||
|
|
||
| prevCount = data->cpus; | ||
| if (count != prevCount) { | ||
| /* free meters for CPUs that are no longer in range */ | ||
| for (unsigned int i = count; i < prevCount; i++) | ||
| Meter_delete((Object*)data->meters[i]); | ||
|
|
||
| if (count > 0) { | ||
| data->meters = xReallocArray(data->meters, count, sizeof(Meter*)); | ||
| /* zero-fill newly added entries */ | ||
| if (count > prevCount) | ||
| memset(data->meters + prevCount, 0, (count - prevCount) * sizeof(Meter*)); | ||
| } else { | ||
| free(data->meters); | ||
| data->meters = NULL; | ||
| } | ||
| data->cpus = count; | ||
| } | ||
|
|
||
| Meter** meters = data->meters; | ||
| for (int i = 0; i < count; i++) { | ||
| for (unsigned int i = 0; i < count; i++) { | ||
| if (!meters[i]) | ||
| meters[i] = Meter_new(this->host, start + i + 1, (const MeterClass*) Class(CPUMeter)); | ||
|
|
||
| Meter_init(meters[i]); | ||
| } | ||
| } | ||
|
|
||
| static void CPUMeterCommonUpdateMode(Meter* this, MeterModeId mode, int ncol) { | ||
| static void AllCPUsMeter_updateValues(Meter* this) { | ||
| CPUMeterData* data = this->meterData; | ||
| unsigned int start, count; | ||
| AllCPUsMeter_getRange(this, &start, &count); | ||
| /* Reinit if the number of CPUs changed (e.g. hot-plug) */ | ||
| if (count != data->cpus) | ||
| CPUMeterCommonInit(this); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| data = this->meterData; | ||
| Meter** meters = data->meters; | ||
| for (unsigned int i = 0; i < count; i++) | ||
| Meter_updateValues(meters[i]); | ||
| } | ||
|
|
||
| static void CPUMeterCommonUpdateMode(Meter* this, MeterModeId mode, unsigned int ncol) { | ||
| /* Reinit first in case the CPU count changed since last init */ | ||
| CPUMeterCommonInit(this); | ||
| CPUMeterData* data = this->meterData; | ||
| Meter** meters = data->meters; | ||
| this->mode = mode; | ||
| int start, count; | ||
| unsigned int start, count; | ||
| AllCPUsMeter_getRange(this, &start, &count); | ||
| if (!count) { | ||
| this->h = 1; | ||
| return; | ||
| } | ||
| for (int i = 0; i < count; i++) { | ||
| for (unsigned int i = 0; i < count; i++) { | ||
| Meter_setMode(meters[i], mode); | ||
| } | ||
| int h = meters[0]->h; | ||
|
|
@@ -303,9 +327,7 @@ static void CPUMeterCommonUpdateMode(Meter* this, MeterModeId mode, int ncol) { | |
| static void AllCPUsMeter_done(Meter* this) { | ||
| CPUMeterData* data = this->meterData; | ||
| Meter** meters = data->meters; | ||
| int start, count; | ||
| AllCPUsMeter_getRange(this, &start, &count); | ||
| for (int i = 0; i < count; i++) | ||
| for (unsigned int i = 0; i < data->cpus; i++) | ||
| Meter_delete((Object*)meters[i]); | ||
| free(data->meters); | ||
| free(data); | ||
|
|
@@ -327,17 +349,18 @@ static void OctoColCPUsMeter_updateMode(Meter* this, MeterModeId mode) { | |
| CPUMeterCommonUpdateMode(this, mode, 8); | ||
| } | ||
|
|
||
| static void CPUMeterCommonDraw(Meter* this, int x, int y, int w, int ncol) { | ||
| static void CPUMeterCommonDraw(Meter* this, int x, int y, int w, unsigned int ncol) { | ||
| CPUMeterData* data = this->meterData; | ||
| Meter** meters = data->meters; | ||
| int start, count; | ||
| unsigned int start, count; | ||
| AllCPUsMeter_getRange(this, &start, &count); | ||
| int colwidth = w / ncol; | ||
| int diff = w % ncol; | ||
| int nrows = (count + ncol - 1) / ncol; | ||
| for (int i = 0; i < count; i++) { | ||
| int d = (i / nrows) > diff ? diff : (i / nrows); // dynamic spacer | ||
| int xpos = x + ((i / nrows) * colwidth) + d; | ||
| unsigned int nrows = (count + ncol - 1) / ncol; | ||
| for (unsigned int i = 0; i < count; i++) { | ||
| int col = i / nrows; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an implicit cast from |
||
| int d = col > diff ? diff : col; // dynamic spacer | ||
| int xpos = x + (col * colwidth) + d; | ||
| int ypos = y + ((i % nrows) * meters[0]->h); | ||
| meters[i]->draw(meters[i], xpos, ypos, colwidth); | ||
| } | ||
|
|
@@ -359,9 +382,9 @@ static void OctoColCPUsMeter_draw(Meter* this, int x, int y, int w) { | |
| static void SingleColCPUsMeter_draw(Meter* this, int x, int y, int w) { | ||
| CPUMeterData* data = this->meterData; | ||
| Meter** meters = data->meters; | ||
| int start, count; | ||
| unsigned int start, count; | ||
| AllCPUsMeter_getRange(this, &start, &count); | ||
| for (int i = 0; i < count; i++) { | ||
| for (unsigned int i = 0; i < count; i++) { | ||
| meters[i]->draw(meters[i], x, y, w); | ||
| y += meters[i]->h; | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The declaration of
prevCountshould be moved back here. There's no use ofprevCountbefore this line.