Bug 257450

Summary: MALLOC_DEFINE macro error in Freebsd13 OS
Product: Base System Reporter: shamsher <meetshamsher>
Component: kernAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed Not A Bug    
Severity: Affects Only Me CC: markj
Priority: ---    
Version: 13.0-STABLE   
Hardware: amd64   
OS: Any   

Description shamsher 2021-07-27 13:12:29 UTC
# cat test.c 
#include <sys/types.h>
#include <sys/module.h>
#include <sys/systm.h>  
#include <sys/param.h> 
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kthread.h>
#include <sys/unistd.h>
#include <stdlib.h> 

MALLOC_DEFINE(M_NODE, "NODE1", "test Node");
int main() {
    int *ptr;
    ptr = (int*)malloc(sizeof(int), M_NODE ,M_WAITOK)
    printf("ptr=%p\n",ptr);
    return 0;
}


# cc test.c 
test.c:11:23: error: expected identifier
MALLOC_DEFINE(M_NODE, "NODE1", "test Node");
                      ^
test.c:11:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
MALLOC_DEFINE(M_NODE, "NODE1", "test Node");
^
test.c:14:37: error: use of undeclared identifier 'M_NODE'
    ptr = (int*)malloc(sizeof(int), M_NODE ,M_WAITOK)
                                    ^
1 warning and 2 errors generated.
Comment 1 Mark Johnston freebsd_committer freebsd_triage 2021-07-27 13:20:36 UTC
MALLOC_DEFINE is for the kernel's malloc() implementation, not the userspace malloc().  Note that in sys/malloc.h, _KERNEL needs to be defined for the MALLOC_DEFINE macro to be visible.

If you have programming questions, please use the mailing lists (e.g., freebsd-questions, https://lists.freebsd.org/mailman/listinfo/freebsd-questions) or the forums (https://forums.freebsd.org/) to ask, rather than submitting issues to the bug tracker.