ALLOC API

1. wbMalloc

wbMalloc allocates a block of memory based on the size specified by the user.

The function allocates a block of memory of the specified size, as indicated by the user. If the allocation fails, it returns a NULL pointer. Therefore, it is crucial to check that the return value is not NULL before using the allocated memory. The size argument specifies the number of bytes to allocate.

Memory allocated with the wbMalloc() function should typically be freed using the wbFree() function, but it is not mandatory because if the memory is not explicitly freed, it will be automatically released when the wbReturn() function is called. Therefore, unless a significant amount of memory is allocated and needs to be freed during service, it is recommended to let the memory automatically be freed through wbReturn().

  • Prototype

    #include <wbapi.h>
    #include <atmi.h>
    void *wbMalloc(WBSVCINFO *rqst, int size)
  • Return value

    The function returns a pointer to the allocated memory. The returned pointer is a pointer to the first byte of the allocated memory. If the requested size of memory cannot be allocated, a NULL pointer is returned.

  • Example

    <wbmalloc.m>

    *DOMAIN
    webtob1
    
    *NODE
     tmaxh1         WEBTOBDIR="/user2/haninho/webtob",
                    SHMKEY = 54777,
                    DOCROOT="/user2/haninho/webtob/docs",
                    APPDIR="/user2/haninho/webtob/ap",
                    PORT = “7654”
    
    *SVRGROUP
     htmlg          NODENAME = "tmaxh1", SvrType = HTML
     webapg         NODENAME = "tmaxh1", SVRTYPE = WEBSTD
    
    *SERVER
     html            SVGNAME = htmlg, MinProc = 1, MaxProc =2
     wbmalloc     SVGNAME = webapg, MinProc = 1, MaxProc = 2, 
    
    *SERVICE
     test            SVRNAME = wbmalloc
    
    *URI
     wbapi          Uri = "/svct/", Svrtype = WEBSTD
    *EXT
     htm             MimeType = "text/html", SvrType = HTML
    1. Compile the configuration file.

      $wscfl –i wbmalloc.m
    2. Create a service table.

      $wsgst
    3. Write a WBAPI program that handles the request. Here is an example for wbmalloc.c.

       #include <stdio.h>
       #include <usrinc/atmi.h>
       #include <usrinc/wbapi.h>
      
       test(WBSVCINFO *rqst) {
              char *mem = NULL;
              mem = wbMalloc(rqst, 128);
      
              if(mem == NULL)
        {
                      wbPrint(rqst, "<HTML>\n");
                      wbPrint(rqst, "<BODY>\n");
                      wbPrint(rqst, "wbMalloc error");
                      wbPrint(rqst, "</BODY></HTML>");
                      wbReturn(rqst, WBERROR);
              }
      
              strcpy(mem, "Hello, Tmax Soft");
      
              wbPrint(rqst, "<HTML>\n");
              wbPrint(rqst, "<HEAD><TITLE>wbMalloc Test</TITLE> </HEAD>\n");
              wbPrint(rqst, "<BODY>\n");
              wbPrint(rqst, "<H1>wbMalloc Test</H1>\n");
              wbPrint(rqst, "%s", mem);
              wbPrint(rqst, "</BODY>\n");
              wbPrint(rqst, "</HTML>\n");
              wbReturn(rqst, WBSUCCESS);
       }
    4. Compile the C file created with WBAPI using the Makefile file.

      $compile c wbmalloc
    5. The following is the screen where the results are printed.

      image

  • Related functions

    wbFree()

2. wbFree

wbFree frees memory allocated by wbMalloc(). The argument ptr is a pointer to the allocated memory. Calling wbFree() with an invalid pointer not obtained by wbMalloc() or attempting to free the same pointer multiple times will result in an error.

  • Prototype

    #include <wbapi.h>
    #include <atmi.h>
    int wbFree(WBSVCINFO *rqst, void *ptr)
  • Return value

    If executed successfully, wbFree() returns 1. If an error occurs, it returns -1.

  • Example

    <wbfree.m>

    *DOMAIN
    webtob1
    
    *NODE
     tmaxh1         WEBTOBDIR="/user2/haninho/webtob",
                    SHMKEY = 54777,
                    DOCROOT="/user2/haninho/webtob/docs",
                    APPDIR="/user2/haninho/webtob/ap",
                    PORT = “7654”
    
    *SVRGROUP
     htmlg          NODENAME = "tmaxh1", SvrType = HTML
     webapg         NODENAME = "tmaxh1", SVRTYPE = WEBSTD
    
    *SERVER
     html           SVGNAME = htmlg, MinProc = 1, MaxProc =2
     wbfree         SVGNAME = webapg, MinProc = 1, MaxProc = 2, 
    
    *SERVICE
     test           SVRNAME = wbfree
    
    *URI
     wbapi          Uri = "/svct/", Svrtype = WEBSTD
    
    *EXT
     htm            MimeType = "text/html", SvrType = HTML
    1. Compile the configuration file.

      $ wscfl –i wbfree.m
    2. Create a service table.

      $ wsgst
    3. Generate a source file. The following is an example WBAPI program that processes requests, named wbfree.c.

       #include <stdio.h>
       #include <usrinc/atmi.h>
       #include <usrinc/wbapi.h>
       test(WBSVCINFO *rqst)
       {
              char *mem = NULL;
              int rnt;
              mem = wbMalloc(rqst, 128);
              if(mem == NULL)
              {
                      wbPrint(rqst, "<HTML>\n");
                      wbPrint(rqst, "<BODY>\n");
                      wbPrint(rqst, "wbMalloc error");
                      wbPrint(rqst, "</BODY></HTML>");
                      wbReturn(rqst, WBERROR);
              }
              strcpy(mem, "Hello, Tmax Soft");
              wbPrint(rqst, "<HTML>\n");
              wbPrint(rqst, "<HEAD><TITLE>wbFree Test</TITLE> </HEAD>\n");
              wbPrint(rqst, "<BODY>\n");
              wbPrint(rqst, "<H1>wbFree Test</H1>\n");
              wbPrint(rqst, "%s", mem);
              rnt =  wbFree(rqst, mem);
              if(rnt == -1)
                      wbPrint(rqst, "<br>wbFree() is failed<br>");
              wbPrint(rqst, "<br>wbFree() is successful<br>");
              wbPrint(rqst, "</BODY>\n");
              wbPrint(rqst, "</HTML>\n");
              wbReturn(rqst, WBSUCCESS);
       }
    4. Compile the C file created with WBAPI using the Makefile file.

      $compile c wbfree
    5. The following is the screen where the results are printed.

      image

  • Related functions

    wbMalloc()