2020-04-09 03:32:11 -05:00
|
|
|
#include "lscpu-api.h"
|
|
|
|
|
|
|
|
struct lscpu_cpu *lscpu_new_cpu(void)
|
|
|
|
{
|
|
|
|
struct lscpu_cpu *cpu;
|
|
|
|
|
|
|
|
cpu = xcalloc(1, sizeof(struct lscpu_cpu));
|
|
|
|
cpu->refcount = 1;
|
2020-04-09 05:10:43 -05:00
|
|
|
cpu->logical_id = -1;
|
2020-04-09 03:32:11 -05:00
|
|
|
|
|
|
|
DBG(CPU, ul_debugobj(cpu, "alloc"));
|
|
|
|
return cpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lscpu_ref_cpu(struct lscpu_cpu *cpu)
|
|
|
|
{
|
|
|
|
if (cpu)
|
|
|
|
cpu->refcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lscpu_unref_cpu(struct lscpu_cpu *cpu)
|
|
|
|
{
|
|
|
|
if (!cpu)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (--cpu->refcount <= 0) {
|
|
|
|
DBG(CPU, ul_debugobj(cpu, " freeing"));
|
|
|
|
lscpu_unref_cputype(cpu->type);
|
|
|
|
free(cpu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int lscpu_add_cpu(struct lscpu_cxt *cxt,
|
|
|
|
struct lscpu_cpu *cpu,
|
|
|
|
struct lscpu_cputype *ct)
|
|
|
|
{
|
|
|
|
struct lscpu_cputype *type;
|
|
|
|
|
2020-04-09 05:10:43 -05:00
|
|
|
/* make sure the type exists */
|
|
|
|
if (ct)
|
|
|
|
type = lscpu_add_cputype(cxt, ct);
|
|
|
|
else
|
|
|
|
type = lscpu_cputype_get_default(cxt);
|
2020-04-09 03:32:11 -05:00
|
|
|
|
|
|
|
cxt->cpus = xrealloc(cxt->cpus, (cxt->ncpus + 1)
|
|
|
|
* sizeof(struct lscpu_cpu *));
|
|
|
|
|
|
|
|
cxt->cpus[cxt->ncpus] = cpu;
|
|
|
|
cxt->ncpus++;
|
|
|
|
lscpu_ref_cpu(cpu);
|
|
|
|
|
2020-04-09 05:10:43 -05:00
|
|
|
if (type) {
|
|
|
|
cpu->type = type;
|
|
|
|
lscpu_ref_cputype(type);
|
|
|
|
}
|
2020-04-09 03:32:11 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-04-09 05:10:43 -05:00
|
|
|
|
|
|
|
int lscpu_cpus_apply_type(struct lscpu_cxt *cxt, struct lscpu_cputype *type)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < cxt->ncpus; i++) {
|
|
|
|
struct lscpu_cpu *cpu = cxt->cpus[i];
|
|
|
|
|
|
|
|
if (!cpu->type) {
|
|
|
|
cpu->type = type;
|
|
|
|
lscpu_ref_cputype(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|