ALLOC API

1. wbMalloc

wbMalloc는 사용자가 설정한 size만큼의 메모리를 할당한다.

wbMalloc() 함수는 사용자가 설정한 size만큼 메모리의 크기를 설정하여 할당한다. 이때 설정한 size만큼의 메모리를 할당할 수 없을 경우에는 NULL포인터를 반환한다. 따라서 이 함수에 의해 할당된 크기의 메모리를 사용하기 위해서는 반환값이 NULL이 아니라는 것을 확인하는 것이 중요하다. 인수 size 는 할당하고자 하는 메모리의 크기이다.

wbMalloc() 함수로 할당된 메모리는 wbFree() 함수를 이용하여 제거(free)시키도록 되어 있으나 wbMalloc() 함수를 이용하여 할당된 메모리를 반드시 wbFree() 함수로 제거(free)시켜야 하는 것은 아니다. wbMalloc() 함수로 할당된 메모리는 wbReturn() 함수가 호출될 때 자동으로 제거(free)되므로 많은 양의 메모리를 할당하여 서비스 도중에 메모리를 제거(free)해야 하는 경우가 아니라면 wbReturn() 함수를 이용하여 메모리를 자동으로 제거(free)할 것을 권장한다.

  • 프로토타입

    #include <wbapi.h>
    #include <atmi.h>
    void *wbMalloc(WBSVCINFO *rqst, int size)
  • 반환값

    할당된 메모리에 대한 포인터를 반환한다. 이때 반환되는 포인터는 할당된 메모리의 첫 번째 바이트에 대한 포인터이다. 만일 요구한 size만큼의 메모리를 할당할 수 없을 경우에는 NULL 포인터를 반환한다.

  • 예제

    <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. 환경파일(Config-file)을 컴파일한다.

      $wscfl –i wbmalloc.m
    2. 서비스 테이블을 생성한다.

      $wsgst
    3. 요청을 처리하는 WBAPI 프로그램을 작성한다. 다음은 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. WBAPI로 만들어진 c파일을 Makefile 파일을 이용하여 컴파일한다.

      $compile c wbmalloc
    5. 다음은 결과가 출력된 화면이다.

      image

  • 관련 함수

    wbFree()

2. wbFree

wbFree는 wbMalloc()에 의해 할당된 메모리를 제거(free)한다. 인수 ptr은 이전에 wbMalloc()에 의해 얻어진 메모리에 대한 포인터이다. 이때 wbMalloc() 함수에 의해 얻어지지 않은 유효하지 않은 포인터를 사용하여 wbFree() 함수를 호출하거나 이미 제거된 메모리 포인터에 대해 다시 한번 wbFree() 함수를 호출하면 에러를 발생시킨다.

  • 프로토타입

    #include <wbapi.h>
    #include <atmi.h>
    int wbFree(WBSVCINFO *rqst, void *ptr)
  • 반환값

    정상적으로 수행될 경우 wbFree()은 1을 반환한다. 에러 발생시에는 –1을 반환한다.

  • 예제

    <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. 환경파일(Config-file)을 컴파일한다.

      $ wscfl –i wbfree.m
    2. 서비스 테이블을 생성한다.

      $ wsgst
    3. 소스 파일을 생성한다. 다음은 요청을 처리하는 WBAPI 프로그램 예제로 파일명은 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. WBAPI로 만들어진 c파일을, Makefile 파일을 이용하여 컴파일한다.

      $compile c wbfree
    5. 다음은 결과가 출력된 화면이다.

      image

  • 관련 함수

    wbMalloc()