包皮瘙痒用什么药| 8月10号什么星座| 静脉曲张有什么症状| 压车是什么意思| 身上有红点是什么病| 更年期失眠吃什么药调理效果好| 羟基丁酸在淘宝叫什么| 子宫内膜炎有什么症状| 超前点映什么意思| 机票什么时候买最便宜| 牙结石长什么样| 什么的歌| 哪吒他妈叫什么名字| 贫血挂什么科| 鼻子发干是什么原因造成的| 银行卡睡眠状态是什么意思| 1985年属什么| 6月17号是什么星座| 胃寒湿气重吃什么药效果最好| 栗子不能和什么一起吃| 更年期吃什么| 一个胸大一个胸小是什么原因| 脱发缺什么维生素| 猫爪草有什么功效| 一醉方休什么意思| 什么情况下打破伤风| 意有所指是什么意思| 防晒霜和隔离霜有什么区别| 仔仔是什么意思| 木须肉为什么叫木须肉| 舐犊是什么意思| 马华念什么| 兔跟什么生肖配对最好| 血清果糖胺测定是什么| 睡眠障碍是什么原因引起的| 纪梵希属于什么档次| 碱水对人有什么危害| 关羽使用的武器是什么| 发量少适合什么发型| 红苋菜不能和什么一起吃| 吃东西恶心想吐是什么原因| 阿尔卑斯是什么意思| 九月初九是什么节日| 什么病| 周文王叫什么名字| 尿崩症是什么意思| 平坦的反义词是什么| 七月十四号是什么星座| 抑郁什么意思| 抗核抗体谱检测查什么的| 河图洛书是什么意思| mmol是什么单位| 如如不动什么意思| 脚酸疼是什么原因引起的吗| 点到为止是什么意思| 什么水果是钙中之王| 一路繁花的意思是什么| 7月24日是什么星座| 为什么会得湿疹| 胳膊肘疼痛是什么原因| 西瓜能做什么美食| 法图麦是什么意思| 澜字五行属什么| 9月30日什么星座| 什么水越洗越脏| 坐飞机什么东西不能带| 为什么人一瘦就会漂亮| 小肠气有什么症状| 晚上三点是什么时辰| 痔疮吃什么食物| 吃什么菜对肝好怎么养肝| 老人脚肿吃什么药消肿| 电话正在通话中是什么意思| 3月16号是什么星座| 提篮子是什么意思| 神经元是什么| 家有蝙蝠是什么兆头| 专科学什么专业好就业| 镇党委副书记是什么级别| 盆腔炎做什么检查| 女人阳虚吃什么药效果最好| 肾的作用和功能是什么| 牙酸是什么原因| 小便失禁是什么原因| 喝豆腐脑有什么好处和坏处| 靠腰是什么意思| 腰扭伤挂什么科| 类风湿要吃什么药| 怀孕是什么脉象| 康字五行属什么| 监视是什么意思| 消字号是什么意思| 上嘴唇发白是因为什么原因| 拔智齿需要注意什么| 石钟乳是什么| 枸杞是补什么的| 老公工作劳累炖什么汤| 脑梗前有什么征兆| 唾液酸酶阳性是什么意思| 73岁属什么| 莫西沙星片主治什么病| 中央民族大学什么档次| 鱼腥草有什么功效| 虾仁不能和什么食物一起吃| 乳腺发炎吃什么消炎药| 什么是人肉搜索| 梦见盗墓是什么意思| 名落孙山的意思是什么| 马的尾巴有什么作用| hm是什么| 将至是什么意思| 门对门有什么说法| 验血脂挂什么科| 樱桃和车厘子有什么区别| 癫痫挂什么科| 3月28号是什么星座| ls是什么牌子| 内分泌失调是什么意思| 杜鹃花什么时候开花| 牛吃什么| 男性裆部瘙痒用什么药好| 养肝吃什么食物| 银杏果长什么样| 嗓子苦是什么原因引起的| 催供香是什么意思| 八院是什么医院| 胎儿缺氧孕妇会有什么反应| 长一根白眉毛预示什么| 痛风是什么原因| 妈妈最大的愿望是什么| 警告处分有什么影响| 浸润性癌是什么意思| 奇可以加什么偏旁| 失责是什么意思| 中央候补委员是什么级别| 梦见佛像是什么意思| 月经期头晕是什么原因| 什么是普拉提| 故人是什么意思| t波改变是什么意思| 众所周知是什么生肖| 决明子泡水喝有什么功效| KTV服务员主要做什么| 高山仰止是什么意思| 烂苹果气味的是什么病| 什么是骨折| 身主天相是什么意思| 什么鱼刺少好吃| 喝酒吃头孢有什么反应| 剖腹产第三天可以吃什么| 清胃火吃什么药| 肿瘤标志物五项检测是什么| 为什么男怕招风耳| 督察是什么意思| 三色线分别代表什么| 孕妇佩戴什么保胎辟邪| 菊花什么时候开| 近视是什么原因造成的| 去黄疸吃什么药| 狗狗肠胃炎吃什么药| 好难过这不是我要的结果什么歌| 浇头是什么意思| 三点水加亘念什么| 龙蛇混杂是什么生肖| 霉菌性阴炎用什么药止痒效果好| 纯爱是什么意思| 尿道尿血是什么原因| 孕妇为什么不能吃西瓜| 吃蒸苹果有什么好处| 为什么会得前列腺炎| 射精太快吃什么好| 才高八斗是指什么生肖| 晚上8点半是什么时辰| 齿加禹念什么| 伏特加兑什么饮料好喝| 正桃花是什么意思| 眼皮痒是什么原因| tdp是什么意思| 在家里做什么能赚钱| 三个鱼读什么| 头发油的快是什么原因| 净高是什么意思| 长江学者是什么级别| 肾阳虚吃什么食物| dsa是什么检查| 哈喇子是什么意思| 桑葚和枸杞泡水喝有什么好处| 人乳头瘤病毒56型阳性是什么意思| 权衡利弊的意思是什么| 橄榄色是什么颜色| 慢性扁桃体炎吃什么药| 什么是桥本病| 章鱼的血是什么颜色| 饱不洗头饿不洗澡是为什么| 肌肉抽筋是什么原因| 长痘痘擦什么药膏好| 阴盛格阳是什么意思| 虫洞是什么| 亚米是什么意思| 容易感冒是什么原因| 吸允的读音是什么| 多囊有什么危害| 盗汗是什么症状| 夫妇是什么意思| 什么植物好养又适合放在室内| 什么是重力| 试管是什么| 珩是什么意思| 喝醉是什么感觉| 大学学什么| 低烧挂什么科| 出色的什么| 想吃肉是身体缺什么| 猪利是什么| 什么黄河| 吃什么会变黑| 溪水什么| 早餐吃什么营养又健康| 维生素b族为什么不能晚上吃| 2月2日什么星座| 新房送什么礼物好| 考级有什么用| 伤口溃烂不愈合用什么药| 安道尔微信暗示什么| 胡青是什么| 长疖子用什么药| 冬瓜有什么功效| 籍贯是填什么| 嫣字五行属什么| 什么的衣服| b1是什么意思| 什么是工作| 早上八点是什么时辰| 非洲说什么语言| 财神爷供奉什么供品| 关节镜是什么| 什么是僵尸恒星| 痔疮是什么东西| 元辰是什么意思| 性价比高什么意思| 紫米和小米什么关系| 什么人容易得圆锥角膜| 手心经常出汗是什么原因| 今夕何夕是什么意思| 圆脸适合什么短发发型| 肝气不舒吃什么中成药| 50而知天命什么意思| 猛虎下山是什么生肖| 早搏心律不齐吃什么药| psv医学是什么意思| 湿气重能吃什么水果| 肺部硬结灶是什么意思| 后背疼是什么病| 内参是什么意思| 油茶是什么| 肝左叶囊肿是什么意思| 浪蹄子什么意思| 小龙虾吃什么| em是什么意思| 9月19是什么星座| 手冲是什么意思| 前途是什么意思| 一个提手一个京念什么| 腱鞘炎吃什么药好使| 百度Jump to content

弗吉尼亚州的费尔法克斯县——城市与乡村的完美结合

From Wikipedia, the free encyclopedia
A ring showing, conceptually, a circular buffer. This visually shows that the buffer has no real end and it can loop around the buffer. However, since memory is never physically created as a ring, a linear representation is generally used as is done below.
百度 长期过量食用还可能引起肝肾脏的慢性损害。

In computer science, a circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.[1] There were early circular buffer implementations in hardware.[2][3]

Overview

[edit]
A 24-byte keyboard circular buffer. When the write pointer is about to reach the read pointer—because the microprocessor is not responding—the buffer stops recording keystrokes. On some computers a beep would be played.

A circular buffer first starts out empty and has a set length. In the diagram below is a 7-element buffer:

Assume that 1 is written in the center of a circular buffer (the exact starting location is not important in a circular buffer):

Then assume that two more elements are added to the circular buffer — 2 & 3 — which get put after 1:

If two elements are removed, the two oldest values inside of the circular buffer would be removed. Circular buffers use FIFO (first in, first out) logic. In the example, 1 & 2 were the first to enter the circular buffer, they are the first to be removed, leaving 3 inside of the buffer.

If the buffer has 7 elements, then it is completely full:

A property of the circular buffer is that when it is full and a subsequent write is performed, then it starts overwriting the oldest data. In the current example, two more elements — A & B — are added and they overwrite the 3 & 4:

Alternatively, the routines that manage the buffer could prevent overwriting the data and return an error or raise an exception. Whether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular buffer.

Finally, if two elements are now removed then what would be removed is not A & B, but 5 & 6 because 5 & 6 are now the oldest elements, yielding the buffer with:

Uses

[edit]

The useful property of a circular buffer is that it does not need to have its elements shuffled around when one is consumed. (If a non-circular buffer were used then it would be necessary to shift all elements when one is consumed.) In other words, the circular buffer is well-suited as a FIFO (first in, first out) buffer while a standard, non-circular buffer is well suited as a LIFO (last in, first out) buffer.

Circular buffering makes a good implementation strategy for a queue that has fixed maximum size. Should a maximum size be adopted for a queue, then a circular buffer is a completely ideal implementation; all queue operations are constant time. However, expanding a circular buffer requires shifting memory, which is comparatively costly. For arbitrarily expanding queues, a linked list approach may be preferred instead.

In some situations, overwriting circular buffer can be used, e.g. in multimedia. If the buffer is used as the bounded buffer in the producer–consumer problem then it is probably desired for the producer (e.g., an audio generator) to overwrite old data if the consumer (e.g., the sound card) is unable to momentarily keep up. Also, the LZ77 family of lossless data compression algorithms operates on the assumption that strings seen more recently in a data stream are more likely to occur soon in the stream. Implementations store the most recent data in a circular buffer.

Circular buffer mechanics

[edit]
Circular buffer implementation in hardware, US patent 3979733, fig4

A circular buffer can be implemented using a pointer and four integers:[4]

  • buffer start in memory
  • buffer capacity (length)
  • write to buffer index (end)
  • read from buffer index (start)

This image shows a partially full buffer with Length = 7:

This image shows a full buffer with four elements (numbers 1 through 4) having been overwritten:

In the beginning the indexes end and start are set to 0. The circular buffer write operation writes an element to the end index position and the end index is incremented to the next buffer position. The circular buffer read operation reads an element from the start index position and the start index is incremented to the next buffer position.

The start and end indexes alone are not enough to distinguish between buffer full or empty state while also utilizing all buffer slots,[5] but can be if the buffer only has a maximum in-use size of Length ? 1.[6] In this case, the buffer is empty if the start and end indexes are equal and full when the in-use size is Length ? 1. Another solution is to have another integer count that is incremented at a write operation and decremented at a read operation. Then checking for emptiness means testing count equals 0 and checking for fullness means testing count equals Length.[7]

The following source code is a C implementation together with a minimal test. Function put() puts an item in the buffer, function get() gets an item from the buffer. Both functions take care about the capacity of the buffer :

#include <stdio.h>

enum { N = 10 };  // size of circular buffer

int buffer [N]; // note: only (N - 1) elements can be stored at a given time
int writeIndx = 0;
int readIndx  = 0;

int put (int item) 
{
  if ((writeIndx + 1) % N == readIndx)
  {
     // buffer is full, avoid overflow
     return 0;
  }
  buffer[writeIndx] = item;
  writeIndx = (writeIndx + 1) % N;
  return 1;
}

int get (int * value) 
{
  if (readIndx == writeIndx)
  {
     // buffer is empty
     return 0;
  }

  *value = buffer[readIndx];
  readIndx = (readIndx + 1) % N;
  return 1;
}

int main ()
{
  // test circular buffer
  int value = 1001;
  while (put (value ++));
  while (get (& value))
     printf ("read %d\n", value);
  return 0;
}

Optimization

[edit]

A circular-buffer implementation may be optimized by mapping the underlying buffer to two contiguous regions of virtual memory.[8][disputeddiscuss] (Naturally, the underlying buffer‘s length must then equal some multiple of the system’s page size.) Reading from and writing to the circular buffer may then be carried out with greater efficiency by means of direct memory access; those accesses which fall beyond the end of the first virtual-memory region will automatically wrap around to the beginning of the underlying buffer. When the read offset is advanced into the second virtual-memory region, both offsets—read and write—are decremented by the length of the underlying buffer.

Fixed-length-element and contiguous-block circular buffer

[edit]

Perhaps the most common version of the circular buffer uses 8-bit bytes as elements.

Some implementations of the circular buffer use fixed-length elements that are bigger than 8-bit bytes—16-bit integers for audio buffers, 53-byte ATM cells for telecom buffers, etc. Each item is contiguous and has the correct data alignment, so software reading and writing these values can be faster than software that handles non-contiguous and non-aligned values.

Ping-pong buffering can be considered a very specialized circular buffer with exactly two large fixed-length elements.

The bip buffer (bipartite buffer) is very similar to a circular buffer, except it always returns contiguous blocks which can be variable length. This offers nearly all the efficiency advantages of a circular buffer while maintaining the ability for the buffer to be used in APIs that only accept contiguous blocks.[9]

Fixed-sized compressed circular buffers use an alternative indexing strategy based on elementary number theory to maintain a fixed-sized compressed representation of the entire data sequence.[10]

References

[edit]
  1. ^ Arpaci-Dusseau, Remzi H.; Arpaci-Dusseau, Andrea C. (2014), Operating Systems: Three Easy Pieces [Chapter: Condition Variables, figure 30.13] (PDF), Arpaci-Dusseau Books
  2. ^ Hartl, Johann (17 October 2011). "Impulswiederholer - Telephone Exchange (video)". Youtube. Retrieved 15 December 2021.
  3. ^ Fraser, Alexander Gibson. "US patent 3979733 Digital data communications system packet switch". US States Patent. Retrieved 15 December 2021.
  4. ^ Liu, Z.; Wu, F.; Das, S.K. (2021). Wireless Algorithms, Systems, and Applications: 16th International Conference, WASA 2021, Nanjing, China, June 25–27, 2021, Proceedings, Part II. Lecture Notes in Computer Science. Springer International Publishing. p. 117. ISBN 978-3-030-86130-8. Retrieved 2025-08-06.
  5. ^ Chandrasekaran, Siddharth (2025-08-06). "Implementing Circular/Ring Buffer in Embedded C". Embed Journal. EmbedJournal Team. Archived from the original on 11 February 2017. Retrieved 14 August 2017.
  6. ^ Circular buffers kernel.org
  7. ^ Morin, Pat. "ArrayQueue: An Array-Based Queue". Open Data Structures (in pseudocode). Archived from the original on 31 August 2015. Retrieved 7 November 2015.
  8. ^ Mike Ash (2025-08-06). "mikeash.com: Friday Q&A 2025-08-06: Ring Buffers and Mirrored Memory: Part II". mikeash.com. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  9. ^ Simon Cooke (2003), "The Bip Buffer - The Circular Buffer with a Twist"
  10. ^ Gunther, John C. (March 2014). "Algorithm 938: Compressing circular buffers". ACM Transactions on Mathematical Software. 40 (2): 1–12. doi:10.1145/2559995. S2CID 14682572.
[edit]
疝气有什么症状 慢悠悠的近义词是什么 什么是琥珀 八朵玫瑰花代表什么意思 甲醇对人体有什么伤害
一个月没有来月经是什么原因 热敷肚子有什么好处 中医湿气重是什么意思 新生儿便秘吃什么好 什么是处女
发烧适合吃什么水果 头发变棕色是什么原因 肺炎是什么原因引起的 noisy是什么意思 6.25是什么星座
激情什么意思 草代表什么生肖 梦见初恋男友是什么意思 急性荨麻疹用什么药 上皮细胞什么意思
aosc是什么病hcv9jop3ns0r.cn 惜字如金是什么意思hcv7jop6ns9r.cn 肾阴虚的症状吃什么药hcv7jop9ns5r.cn 什么血型招蚊子咬hcv8jop3ns5r.cn rfc是什么意思hcv7jop6ns0r.cn
鬼畜什么意思zsyouku.com 什么是超度hcv8jop4ns1r.cn 高大尚是什么意思hcv9jop7ns5r.cn 透析病人磷高了吃什么降磷hcv8jop4ns2r.cn 海豚吃什么食物dayuxmw.com
秦始皇是什么民族hcv9jop7ns2r.cn 郫县豆瓣酱能做什么菜hcv7jop7ns1r.cn 肺炎是什么bjhyzcsm.com 什么是黑色素肿瘤hcv8jop8ns4r.cn yuki是什么意思hcv8jop4ns4r.cn
slogan什么意思hcv7jop6ns8r.cn 静态纹用什么除皱hcv9jop4ns3r.cn 智多星是什么意思hcv8jop1ns6r.cn 头胀痛什么原因hcv7jop9ns7r.cn 薛定谔的猫比喻什么hcv9jop1ns5r.cn
百度