Skip to contentSkip to conversation

Match and print size of allocation

cocci Yudi Yang 2 participants 3 messages Apr 15, 2023 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): eccc2924-6657-4776-84f1-3d8413904cf2@Spark

Yudi Yang

author
#1 lore raw

Hello,

I would like to match the size of allocation, I am wondering if I write the following:

@print@
expression N, SIZE;
@@

(
  malloc(SIZE);
|
  calloc(N, SIZE);
)
+ printf(“Allocation: %d\n", (N) * (SIZE));

I am not sure if I could set the default value for N in the malloc case, or is there anything I should do to make it correct.

Best,
Yudi

#2 lore raw
Quoted 8 lines · Yudi Yang

Hello,

I would like to match the size of allocation, I am wondering if I write the following:

@print@
expression N, SIZE;
@@

Quoted 8 lines

(
  malloc(SIZE);
|
  calloc(N, SIZE);
)
+ printf(“Allocation: %d\n", (N) * (SIZE));

I am not sure if I could set the default value for N in the malloc case, or is there anything I should do to make it correct.

Hello,

I am not sure what your concern is. Normally, you can't put a + after a disjunction like this. You should duplicate the addition in each branch of the disjunction. N won't be bound in the malloc case, so you won't be able to use it there.

julia

#3 lore raw
In reply to Yudi Yang
Quoted 10 lines · Yudi Yang

Hello,

I would like to match the size of allocation, I am wondering if I write the following:

@print@
expression N, SIZE;
@@

(
  malloc(SIZE);

Code doesn't usually contain just malloc(...); because then the return value would be thrown away. You can write x = malloc(SIZE); for an expression metavariable x. If malloc can be used in other contexts, eg an if header, then you can write malloc(SIZE)@S where S is a statement metavariable. I think that that will allow the printf to be added after the innermost statement containing the call to malloc.

julia

Quoted 11 lines

|
  calloc(N, SIZE);
)
+ printf(“Allocation: %d\n", (N) * (SIZE));

I am not sure if I could set the default value for N in the malloc case, or is there anything I should do to make it correct.

Best, Yudi