Author Topic: Fixed and local memory macros.  (Read 2208 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7448
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Fixed and local memory macros.
« on: December 17, 2014, 10:09:42 AM »
Beneath the hood PowerBASIC does some interesting stuff when it comes to initialised and uninitialised data, when you write any data content, it ends up in the initialised data section but interestingly enough when you just allocate memory without content, it goes into the uninitialised data section (commonly called the BSS section). Now this means that if you want a spare meg to play with, you can add it into the uninitialised data section without blowing out the file size but when you compile an exe using it, the extra memory shows up as increased size of the memory image.

Below are 2 sets of macros, the GLOBAL memory kind and a matching pair for LOCAL memory allocated on the stack. With either of the string allocation macros, if you assign the result to a variable declared as a STRINGZ PTR you can use the normal pointer notation ( @myvar ) for basic functions that want a string yet you can also reference the memory as an address.

More often than not you would use normal allocated dynamic memory Global/Virtual/HeapAlloc() but this is a viable option if you need a fixed amount of memory for the duration of an application. The LOCAL macros are useful when you don't need large amounts and you only need it in local scope.

  ' ---------------------------------------------------------
  ' allocate GLOBAL memory and assign a string to it
  ' For an empty buffer use "" for the string
  ' ---------------------------------------------------------
    MACRO FUNCTION globalstr(quoted_text,bytecount)
      MACROTEMP globalstring
      GLOBAL globalstring as STRING * bytecount
      globalstring = quoted_text
    END MACRO = VarPtr(globalstring)

  ' ---------------------------------------------------------
  ' allocate fixed memory in the executable BSS section
  ' ---------------------------------------------------------
    MACRO FUNCTION fixedmem(bytecount)
      MACROTEMP bss_section_memory
      GLOBAL bss_section_memory as STRING * bytecount
    END MACRO = VarPtr(bss_section_memory)

  ' ---------------------------------------------------------
  ' allocate local memory and assign a string to it
  ' For an empty buffer use "" for the string
  ' ---------------------------------------------------------
    MACRO FUNCTION localstr(quoted_text,bytecount)
      MACROTEMP szstring
      LOCAL szstring as STRINGZ * bytecount
      szstring = quoted_text
    END MACRO = VarPtr(szstring)

  ' ---------------------------------------------------------
  ' allocate local memory and return its address
  ' ---------------------------------------------------------
    MACRO FUNCTION localmem(bytecount)
      MACROTEMP stack_memory
      LOCAL stack_memory as STRING * bytecount
    END MACRO = VarPtr(stack_memory)
  ' ---------------------------------------------------------
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: