memory.h 2.1 KB
Newer Older
1 2 3 4
#pragma once

#include <eoslib/types.h>

A
Andrianto Lie 已提交
5 6 7 8 9 10
/**
 *  @defgroup memoryapi Memory API
 *  @brief Defines common memory functions
 *  @ingroup contractdev
 */

11 12 13 14
extern "C" {
  /**
   *  @defgroup memorycapi Memory C API
   *  @brief Defines common memory functions
A
Andrianto Lie 已提交
15
   *  @ingroup memoryapi
16 17 18 19
   *
   *  @{
   */

B
Brian Johnson 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 /**
   * Allocate or remove a page of memory to accommodate the
   * requested number of bytes.
   * @brief Allocate or remove a page of memory
   * @param num_bytes  Number of bytes to add or remove.
   *
   * @return void pointer to the previous end of allocated bytes
   *
   * Example:
   * @code
   * // allocate a whole new page, the returned offset is the pointer to the
   * // newly allocated page (if passing in negative num_bytes, then offset
   * // will be the end of the page that was removed
   * char* new_page = static_cast<char*>(sbrk(65536));
   * memset(new_page, 0, 65536);
   * @endcode
   */
  void* sbrk( int32_t num_bytes );

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 /**
  * Copy a block of memory from source to destination.
  * @brief Copy a block of memory from source to destination.
  * @param destination  Pointer to the destination to copy to.
  * @param source       Pointer to the source for copy from.
  * @param num          Number of bytes to copy.
  *
  * @return the destination pointer
  *
  * Example:
  * @code
  * char dest[6] = { 0 };
  * char source[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };
  * memcpy(dest, source, 6 * sizeof(char));
  * prints(dest); // Output: Hello
  * @endcode
  */
  void* memcpy( void* destination, const void* source, uint32_t num );

  /**
   * Fill block of memory.
   * @brief Fill a block of memory with the provided value.
   * @param ptr    Pointer to memory to fill.
   * @param value  Value to set (it is passed as an int but converted to unsigned char).
   * @param num    Number of bytes to be set to the value.
   *
   * @return the destination pointer
   *
   * Example:
   * @code
   * char ptr[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };
   * memset(ptr, 'y', 6 * sizeof(char));
   * prints(ptr); // Output: yyyyyy
   * @endcode
   */
   void* memset( void* ptr, uint32_t value, uint32_t num );
  /// @}
} // extern "C"