⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 malloc.h

📁 SOS操作系统用于无线传感器网络节点的源代码
💻 H
字号:
#ifndef MALLOC_H
#define MALLOC_H
#include "sos_types.h"
#include "pid.h"
#include "malloc_conf.h"
#include "hardware_proc.h"

#include <stdbool.h>
#include <stdarg.h>

/**
 * @brief Init function for memory manager
 */
extern void mem_init(void);

/**
 * @brief Starting memory module interface
 */
extern void mem_start(void);

/**
 * @brief Allocate a chunk of blocks from the heap
 */
void *sos_blk_mem_alloc(uint16_t size, sos_pid_t id, bool bCallFromModule);

/**
 * @brief Free a block back into the heap
 */
extern void sos_blk_mem_free(void* ptr,bool bCallFromModule);

/**
 * @brief Re-allocate a block of memory from the heap
 */
extern void *sos_blk_mem_realloc(void* pntr, uint16_t newSize, bool bCallFromModule);

/**
 * @brief Change memory ownership of a segment of memory
 */
extern int8_t sos_blk_mem_change_own(void* ptr, sos_pid_t id, bool bCallFromModule);

/**
 * @brief Allocate dynamic memory
 * @param size Number of bytes to allocate
 * @param id Node responsible for the memory
 * @return Returns a pointer to the allocated memory.
 * Will return a NULL pointer if the call to sys_malloc fails.
 */
static inline void *ker_malloc(uint16_t size, sos_pid_t id)
{
  return sos_blk_mem_alloc(size, id, false);
}

/**
 * @brief Reallocate dynamic memory
 * @param pntr Pointer to the currently held block of memory
 * @param newSize Number of bytes to be allocated
 * @return Returns the pointer to the reallocated memory.
 * Returns a NULL if unable to reallocate but the original pointer is still valid.
 */
static inline void* ker_realloc(void* pntr, uint16_t newSize)
{
  return sos_blk_mem_realloc(pntr, newSize, false);
}

/**
 * @brief Free memory pointed to by ptr
 * @param ptr Pointer to the memory that should be released
 * @return void
 */
static inline void ker_free(void* ptr)
{
  sos_blk_mem_free(ptr, false);
  return;
}

/**
 * @brief Change the ownership of memory
 * @param ptr Pointer to the memory whose ownership is being transferred
 * @param id New owner of the memeory
 * @return SOS_OK or error code upon fail
 * Add check to prevent a change of ownership to the 'null' user.
 */
static inline int8_t ker_change_own(void* ptr, sos_pid_t id)
{
  return sos_blk_mem_change_own(ptr, id, false);
}


extern sos_pid_t mem_check_memory();

/**
 * @brief Free up all memory held by id
 * @param id Process that is having its memory returned
 */
extern int8_t mem_remove_all(sos_pid_t id);

/**
 * @brief malloc for long term usage
 * @warning this is used to allocate the memory for long time usage
 */
extern void* malloc_longterm(uint16_t size, sos_pid_t id);


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -