/** * \brief Tries to reads an item from the buffer. * * If the buffer cannot currently be read from, this operation returns * with an appropriate status code. * * \param item out: The read item. If the return code is false, the * value is undefined. * \returns true if an item was read, false otherwise */ inline bool tryRead(register rbdata_t *item) { assert(NULL != item); // loop until the buffer has a full slot if (!canRead()) return false; // fetch fresh _MemoryBarrier(); // since we read, the fill level will be decreased --_fillLevel; // write to the current write index *item = _buffer[_read_index]; // advance the read pointer _read_index = (_read_index+1) & this->_sizeMask; // since we read, the capacity will be increased ++_capacity; // there we go _MemoryBarrier(); return true; }
int main(void) { void *ptrs[6]; uint8_t i; for (i = 0; i < 6; i++) { void *p = malloc(10); if (p == NULL) return __LINE__; ptrs[i] = p; } free(ptrs[5]); _MemoryBarrier(); /* Freelist must be still empty, and __brkval reduced. */ if (__flp != NULL) return __LINE__; if ((char *)(ptrs[5]) - 2 != __brkval) return __LINE__; free(ptrs[4]); _MemoryBarrier(); /* Still no entry, and __brkval further down. */ if (__flp != NULL) return __LINE__; if ((char *)(ptrs[4]) - 2 != __brkval) return __LINE__; struct __freelist *ofp = __flp; free(ptrs[1]); _MemoryBarrier(); /* One entry added. */ if ((char *)(ptrs[1]) - 2 != (void *)__flp) return __LINE__; if (__flp->sz != 10) return __LINE__; if (__flp->nx != (void *)ofp) return __LINE__; free(ptrs[3]); _MemoryBarrier(); /* __brkval lowered again. */ if (__flp->nx != NULL) return __LINE__; if ((char *)(ptrs[3]) - 2 != __brkval) return __LINE__; free(ptrs[2]); _MemoryBarrier(); /* ...and again. */ if (__flp->nx != NULL) return __LINE__; if ((char *)(ptrs[1]) - 2 != __brkval) return __LINE__; return 0; }
/** * \brief Tries to write an item to the buffer. * * If the buffer cannot currently be written to, this operation returns * with an appropriate status code. * * \param item The item to write. * \returns true if the write operation succeeded, false otherwise */ inline bool tryWrite(register const rbdata_t item) { // test if the buffer has a free slot if (!canWrite()) return false; // fetch fresh _MemoryBarrier(); // since we write, the capacity will be reduced --_capacity; // write to the current write index _buffer[_write_index] = item; // advance the write pointer _write_index = (_write_index+1) & this->_sizeMask; // since we write, the fill level will be increased ++_fillLevel; // There we go _MemoryBarrier(); return true; }