<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1051472106557425450</id><updated>2012-02-07T10:38:08.602-08:00</updated><title type='text'>Geeky content...</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>10</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-2917808898313624593</id><published>2012-02-07T02:51:00.000-08:00</published><updated>2012-02-07T10:38:08.617-08:00</updated><title type='text'>Towards an "Evolved Packet Edge"</title><content type='html'>There is a lot of discussion about the "Evolved Packet Core" in the  telecom world these days. However, there is a lot of 'Evolution'  happening on the edge of the mobile internet which gets lesser  attention. In this article I shine some light on the evolution happening  at the edge. There are 2 major themes driving this evolution of the  network edge -&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Small Cells  (Include both the lower  capacity base stations (3G/4G femtocells and picocells) and Wi-Fi access  points in this category) - Given that spectrum is limited and too  expensive, operators have realized they have to include a large number  of these small cells as a part of their network. The inclusion of small  cells in the network leads to a huge increase in the number of "RAN nodes" that the mobile core network needs to talk to, thus disrupting  the scaling equations of their core network elements&lt;br /&gt;&lt;/li&gt;&lt;li&gt;A large  part of the mobile data traffic is video and this traffic is very  latency and jitter sensitive. So, the Content Delivery Networks (CDNs) are becoming  more and more distributed to deliver content from a point nearest to  the end user&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Both of the above points call for a distributed  architecture for the evolved packet core, in order to scale better as  well as to provide a better end user experience. The SAE-EPC  architecture allows for distributing the evolved packet core network  elements and locating the S-GWs and P-GWs closer to the end users.  However, most of these network elements are built on top of the same  hardware platform used for SGSNs and GGSNs (3G core network elements).  Also, in most cases multiple logical elements are physically co-located  on the same box in order to reduce the amount of signaling messages  between the different logical elements in the EPC network. For these reasons, most of the available EPC elements today are better suited for a centralized deployment model.&lt;br /&gt;&lt;/p&gt;While  the EPC elements may eventually become more distributed, it may not  happen in a hurry. So, until then what are the options to deliver a  better end user experience and scaling of the network? The answer seems  to be to move towards a more distributed architecture using  "Edge gateways"  like the Femtocell Gateways and WiFi access gateways that are defined by  3GPP, as shown in the below diagram -&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/-gw8DvbvQ9TU/TzEHwjjlLUI/AAAAAAAAACg/gG6w66twed8/s1600/SmallCellAggregation.jpeg"&gt;&lt;img style="display: block; margin: 0px auto 10px; text-align: center; cursor: pointer; width: 408px; height: 485px;" src="http://3.bp.blogspot.com/-gw8DvbvQ9TU/TzEHwjjlLUI/AAAAAAAAACg/gG6w66twed8/s400/SmallCellAggregation.jpeg" alt="" id="BLOGGER_PHOTO_ID_5706350733614722370" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;p&gt;As shown in the above diagram, these 'Evolved packet edge' devices like the Femtocell Gateways and the WiFi access gateways can be more distributed, and could support "Selective" offloading of the internet traffic to the internet POP directly without routing this traffic to the packet core. &lt;/p&gt;&lt;p&gt;These edge devices will need to talk to the EPC elements for all the control plane signaling, for authenticating the users, for downloading the offload related policies etc. So, the EPC elements will still be fully in charge of the sessions, but then they could decide that a certain set of "Flows" need not be routed all the way to the core network, and decide to offload them away to the internet POP at the edge itself. To enable this selective offloading there are certain standard (SIPTO comes to mind) and a few non standard mechanisms. &lt;/p&gt;&lt;p&gt;So, in a nutshell, by employing these "Evolved packet edge" network elements and distributing them strategically, the operators can gain the the following benefits -&lt;br /&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Reduce the load on their core network elements&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Offer a better end user experience&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Reduce cost by getting rid of routing inefficiencies caused by the centralized approach&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Given the strategic position these edge devices are located in the network, they should be able to participate in the network in many innovative ways in the near future.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-2917808898313624593?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/2917808898313624593/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=2917808898313624593' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/2917808898313624593'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/2917808898313624593'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2012/02/towards-evolved-packet-edge.html' title='Towards an &quot;Evolved Packet Edge&quot;'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-gw8DvbvQ9TU/TzEHwjjlLUI/AAAAAAAAACg/gG6w66twed8/s72-c/SmallCellAggregation.jpeg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-2454012401590012893</id><published>2012-02-01T06:02:00.000-08:00</published><updated>2012-02-01T07:36:24.442-08:00</updated><title type='text'>Handover problem with LTE Femtocells</title><content type='html'>The 3GPP standards (36.413, the S1-AP specification in particular) lack  support for inbound handovers to an LTE Femtocell (a.k.a HeNB), which is  behind an HeNB Gateway. The problem comes while trying to route an S1  Handover Request from the MME to an HeNB, via the HeNB gateway.&lt;br /&gt;&lt;br /&gt;The diagram below illustrates the scenario -&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/--QXYQMlwuOk/TylSVtu8RfI/AAAAAAAAACI/ksCrw4Y8B_c/s1600/s1-handover.jpeg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 262px; height: 400px;" src="http://2.bp.blogspot.com/--QXYQMlwuOk/TylSVtu8RfI/AAAAAAAAACI/ksCrw4Y8B_c/s400/s1-handover.jpeg" alt="" id="BLOGGER_PHOTO_ID_5704180936048657906" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;To initiate the S1 Handover procedure, the source eNB sends an "S1 Handover required" message to the MME. This message has the TAI (Tracking Area Identity) and the target eNB Id in it. Note that the target eNB ID in this case will be unknown to the MME, since that belongs to an HeNB that is "Hidden" from the MME by the HeNB gateway.&lt;/li&gt;&lt;li&gt;If there was no HeNB gateway hiding the target eNB, the MME would simply send an S1 Handover Request to the HeNB directly as indicated by the 'target eNB Id'. But since in this case the target eNB is an HeNB which is hidden behind the HeNB gw, the MME will have to send the S1 Handover request to the HeNB gateway instead&lt;/li&gt;&lt;li&gt;The MME could send the S1 Handover request to the HeNB gateway by using TAI (Which should map directly to the target HeNB gateway) OR alternatively, by using the target eNB Id and a "Subnet" concept. The subnet concept is one in which HeNB Ids contain the HeNB gateway ID also as a part of their ID, so that MME could find out the HeNB gateway ID corresponding to a particular HeNB using the high order bits and masking out the low order bits. So, there are a couple of ways for the MME to find out to which HeNB gateway the S1 Handover Request message should be routed to. So, there are a couple of ways by which the MME finds out which HeNB gateway to route the S1 Handover request to.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;The problem arises when the "S1 Handover Request" reaches the HeNB gateway...&lt;span style="font-weight: bold;"&gt; This message doesn't have any target eNB ID in it!!! &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;So, how would the HeNB gateway route the S1 Handover Request message further down to the actual target HeNB? Unfortunately this is not handled by the 3gpp specifications as of today&lt;/li&gt;&lt;li&gt;The HeNB gateways will be able to route this message to the HeNB via a hack. &lt;/li&gt;&lt;li&gt;The HeNB gateways will have to peep inside the "Source to target &lt;span style="font-weight: bold;"&gt;transparent &lt;/span&gt;container" IE, to get the target cell id from inside it. This breaks layering and renders the "Transparency" of this IE ineffective.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;To solve this problem, the 3gpp S1-AP specifications will need to be updated to include the target eNB Id as part of the "S1 Handover Request" message. Until then the HeNB gateways will need to do with the above workaround.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-2454012401590012893?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/2454012401590012893/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=2454012401590012893' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/2454012401590012893'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/2454012401590012893'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2012/02/handover-problem-with-lte-femtocells.html' title='Handover problem with LTE Femtocells'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/--QXYQMlwuOk/TylSVtu8RfI/AAAAAAAAACI/ksCrw4Y8B_c/s72-c/s1-handover.jpeg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-6227076792351920078</id><published>2008-03-20T02:29:00.000-07:00</published><updated>2008-03-20T03:27:37.862-07:00</updated><title type='text'>Trouble over-riding libc calloc/malloc/free etc</title><content type='html'>Ever tried over-riding the libc allocator functions? I did and the journey was not as smooth as I'd anticipated it to be. My reasoning was that if I implemented the overriding functions malloc/realloc/calloc/memalign/valloc/free etc in my application, then they would get picked ahead of the libc functions by the dynamic linker, since they lie in the main application code itself and the whole program would use these versions instead of the libc functions. My thinking was almost right (The dynamic linker put my versions AHEAD of the libc versions and all dynamic symbol resolution led to my functions getting called - in fact even the libc strdup function that called malloc, ended up calling my version of malloc instead of the libc malloc itself).&lt;br /&gt;&lt;br /&gt;However, there was an exception. One particular function _dl_tls_setup (Implemented in the libdl library) ended up calling the libc-calloc!!! This led to a crash at process shutdown time. At process shutdown, _dl_deallocate_tls tried to free up that memory, but then it called my version of free!!! However, my version of free expected a different header than what libc had put in that chunk and thus crashed the code.&lt;br /&gt;&lt;br /&gt;Why did this happen? Why didn't the libdl library not consult the dynamic linker for resolving the calloc/free symbols? When faced with such questions, its best to study the running code using GDB. I attached GDB to the running process and did the following -&lt;br /&gt;&lt;br /&gt;(You could refer to my &lt;a href="http://docs.google.com/Doc?docid=dcvfwzjw_17n5w8vgz&amp;amp;hl=en"&gt;GDB log file&lt;/a&gt; as you read the steps below)&lt;br /&gt;&lt;br /&gt;1&gt; dis-assembled the function _dl_tls_setup&lt;br /&gt;2&gt; Figured that the call to calloc was consulting the PLT (Procedure linkage table) entries for the libdl library&lt;br /&gt;3&gt; I then dis-assembled the PLT entries at the address mentioned in the previous step&lt;br /&gt;4&gt; That led me to the GOT (Global offset table)  by the libdl library&lt;br /&gt;5&gt; Note that each shared library has its own PLT and GOT entries that has mapping information for each and every dynamic symbol that it uses that is defined by some other library&lt;br /&gt;6&gt; The GOT contained the address of the calloc function that was being referenced by _dl_tls_setup&lt;br /&gt;7&gt; I then did a 'info sharedlibraries' call in GDB which showed the memory mapping information of the entire program&lt;br /&gt;8&gt; From that I could see that the calloc address fell within the address bounds of the LIBC function&lt;br /&gt;9&gt; Doing a similar exercise (Steps 1-8) for the _dl_deallocate_tls function that was calling the 'free', I realized it was calling the free defined by my application code&lt;br /&gt;&lt;br /&gt;Upon posting some questions to 'Linux Dynamic linker' gurus, I finally got the following answer -&lt;br /&gt;&lt;br /&gt;"The libdl library is very special. Since it has to do some allocations before all of its dynamic linker structures are setup, it maps to libc-calloc at startup time without really going through the dynamic symbol resolution step. However, only calloc is called during the phase when the dynamic linker symbol resolution mechanisms are NOT YET set up. By the time it calls 'free' all the dynamic symbol resolution mechanisms are setup already and it ends up picking the 'free' from your application"&lt;br /&gt;&lt;br /&gt;Aaha! So, how did I get around this issue? For now, I have gone for a simple hack - If the process is 'Shutting down' then if 'free' finds some header mismatch, simply return from the function ignoring this chunk. Well, never mind if this memory leaks, since anyways, the whole heap used by the process will be released upon process exit in any case. I'm almost certain that except during the process shutdown period (When the _dl_deallocate_tls gets called) this scenario will never occur.&lt;br /&gt;&lt;br /&gt;Does anyone have a better idea? Or some more gyaan about this topic? It would be interesting to hear from you.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-6227076792351920078?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/6227076792351920078/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=6227076792351920078' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/6227076792351920078'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/6227076792351920078'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2008/03/trouble-over-riding-libc-calloc.html' title='Trouble over-riding libc calloc/malloc/free etc'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-1169636119737982887</id><published>2007-10-18T03:08:00.000-07:00</published><updated>2007-10-18T03:16:24.805-07:00</updated><title type='text'>ESC India 2007</title><content type='html'>The 'Embedded Systems Conference' was held for the first time in Bangalore this year. It was a great success considering that it was being held here for the first time. Industry stalwart Jack Ganssle kicked off a series of interesting technical presentations. I hosted 2 classes this year - 1&gt; C++ Templates and STL and 2&gt; Concurrency design patterns.&lt;br /&gt;&lt;br /&gt;In my past experience I have observed that engineers in the embedded domain shy away from templates for many reasons. So, in my class I explored the many myths surrounding C++ templates and also talked about some real issues with templates and the workarounds for those issues. Then I went on to extol the virtues of generic programming by taking the example of the design of the STL.&lt;br /&gt;&lt;br /&gt;In the other talk, I spoke about the various design patterns in the domain of concurrent software. Much of the material was collected from my past experience in developing highly concurrent networked software for telecom/datacomm products. I talked about the C++ free and open source toolkit called ACE that implemented many concurrency design patterns with some examples of the patterns taken from the toolkit.&lt;br /&gt;&lt;br /&gt;Going by the amount of questions I got from the audience, both during the talk and what followed via e-mail conversations, I think both talks were highly useful to the audience. It was a very satisfying experience for me. Looking forward to ESC India 2008.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-1169636119737982887?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/1169636119737982887/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=1169636119737982887' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/1169636119737982887'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/1169636119737982887'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2007/10/esc-india-2007.html' title='ESC India 2007'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-7796544588764369654</id><published>2007-09-01T07:33:00.000-07:00</published><updated>2007-09-01T07:43:09.259-07:00</updated><title type='text'>C++ Pointers and Memory Management</title><content type='html'>Pointers and memory management form the most error prone area in C++. Thorough knowledge of pointer related pitfalls and traps is a valuable asset for any programmer. The fundamental issue with pointers is that there is no strong coupling between the pointer and what it points to. Pointers could be freely assigned to random memory areas, copied etc, without doing anything about the objects these point to. So, its very easy to end up with memory leaks and corruption. One way we can make pointers safer is to build a strong coupling between pointers and the objects they point to. A popular technique for doing that is called the 'Smart pointer' idiom in C++. Smart pointer based techniques such as scoped pointers, reference counting and copy on write, go a long way in making pointer related code more robust and efficient. I recently gave a talk on this very topic. You can find the slides of this talk at this &lt;a href="http://www.slideshare.net/reachanil/c-memory-management/"&gt;slideshare link&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.digg.com"&gt;&lt;br /&gt;&lt;img src="http://digg.com/img/badges/91x17-digg-button.gif" width="91" height="17" alt="Digg!" /&gt;&lt;br /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-7796544588764369654?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/7796544588764369654/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=7796544588764369654' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/7796544588764369654'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/7796544588764369654'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2007/09/c-pointers-and-memory-management.html' title='C++ Pointers and Memory Management'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-7952334285749114750</id><published>2007-09-01T01:07:00.000-07:00</published><updated>2007-09-01T01:38:15.898-07:00</updated><title type='text'>Static and Dynamic polymorphism in C++</title><content type='html'>C++ supports polymorphism of 2 kinds - polymorphism of types (Templates) and polymorphism of behavior (Virtual methods and inheritance). Sometimes one could simulate polymorphism of types using inheritance (Deriving all types from a common base class and implementing generic algorithms to use base class pointers) and also do polymorphism of behavior using templates (Policy based design). Doing polymorphism of types using inheritance is a highly flawed approach where one loses speed, type safety and convenience. This is done only by users who are too scared to use templates. However, doing polymorphism of behavior using templates, can be a good thing sometimes. Since templates lead to faster and safer code (The abstract - concrete binding happens at compile time instead of at runtime, so faster, and more type safe), one must actively seek to simulate both forms of polymorphism (Types and Behavior) using templates. Of course, there are certain things which absolutely require inheritance and virtual methods. One example is that while designing OO frameworks, often the implementation would need to have 'Heterogeneous containers', and this is not possible using templates. So, in such cases, of course we'd have to employ inheritance and virtual functions. But in many other cases, templates could be employed to simulate both forms of polymorphism and actually leads to faster and safer code. I recently gave a small talk that compared the 2 forms of polymorphism in C++. You can find the slides at this &lt;a href="http://www.slideshare.net/reachanil/static-and-dynamic-polymorphism-in-c/"&gt;slideshare page&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.digg.com/"&gt;&lt;br /&gt;&lt;img src="http://digg.com/img/badges/91x17-digg-button.gif" alt="Digg!" height="17" width="91" /&gt;&lt;br /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-7952334285749114750?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/7952334285749114750/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=7952334285749114750' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/7952334285749114750'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/7952334285749114750'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2007/09/static-and-dynamic-polymorphism-in-c.html' title='Static and Dynamic polymorphism in C++'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-4351911379310601061</id><published>2007-08-08T09:14:00.000-07:00</published><updated>2007-08-08T09:52:53.895-07:00</updated><title type='text'>C++: Finding Fibonacci sequence number in ZERO time</title><content type='html'>C++ template meta-programming allows us to write many recursive programs that have ZERO runtime cost (Obviously it can't get any faster than that!!). Well, the secret here is that all the work is done at compile time, using template techniques (non-type parameters and template specialization). Check out the small program below that finds the fibonacci sequence number for an input index -&lt;br /&gt;&lt;br /&gt;template &amp;lt; int N &amp;gt; &lt;int n=""&gt;&lt;int&gt; &lt;int&gt; &lt;int n=""&gt;&lt;br /&gt;class Fibonacci&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public:&lt;br /&gt;&lt;br /&gt;enum {value = (Fibonacci&amp;lt;N-1&amp;gt;&lt;n-1&gt;&lt;n-1&gt;::value + Fibonacci&lt;n-2&gt;&lt;n-2&gt;&lt;/n-2&gt;&lt;/n-2&gt;&lt;/n-1&gt;&lt;/n-1&gt;&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&amp;lt;N-2&amp;gt;&lt;int n=""&gt;&lt;int&gt;&lt;int&gt;&lt;int n=""&gt;&lt;n-1&gt;&lt;n-1&gt;&lt;n-2&gt;&lt;n-2&gt;::value)};&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;template &lt;&gt;&lt;br /&gt;class Fibonacci&lt;1&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public:&lt;br /&gt;&lt;br /&gt;enum {value = 1};&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;template &lt;&gt;&lt;br /&gt;class Fibonacci&lt;0&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;public:&lt;br /&gt;&lt;br /&gt;enum {value = 1};&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;Now, in your programs, you just have to say - Fibonacci&lt;20&gt;, Fibonacci&lt;15&gt; etc and you shall get the answer in a jiffy (actually, in no time at all!).&lt;br /&gt;&lt;br /&gt;This is just an example program, try out more experiments yourself to explore the power of template meta-programming. Of course, the flip side to zero runtime cost, is the larger code size, longer compile times etc...but hey! You got to give some to get some.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.digg.com"&gt;&lt;br /&gt;&lt;img src="http://digg.com/img/badges/91x17-digg-button.gif" width="91" height="17" alt="Digg!" /&gt;&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/n-2&gt;&lt;/n-2&gt;&lt;/n-1&gt;&lt;/n-1&gt;&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-4351911379310601061?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/4351911379310601061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=4351911379310601061' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/4351911379310601061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/4351911379310601061'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2007/08/c-finding-fibonacci-sequence-number-in.html' title='C++: Finding Fibonacci sequence number in ZERO time'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-5022515344573618504</id><published>2007-07-21T07:49:00.000-07:00</published><updated>2007-07-21T09:15:16.050-07:00</updated><title type='text'>Packet Processor Lookup design</title><content type='html'>In software based packet routers, there is a separation of data plane and control plane. While the control plane is responsible for setting up and tearing down packet data sessions using some end-to-end signaling with end devices, the data plane is responsible for routing/forwarding packets.&lt;br /&gt;&lt;br /&gt;The control plane sets up the session data structures which has to be looked up by the data plane during packet processing. This data has to be shared between the control plane and the data plane in a thread-safe manner, while keeping the lookup times short.&lt;br /&gt;&lt;br /&gt;I came up with a design that aims to minimize the lock contention and to shrink the search space to enable fast and safe lookups of the data. You can find the design in this &lt;a href="http://docs.google.com/Doc?id=dcvfwzjw_12mjjv7s"&gt;&lt;span style="font-weight: bold;"&gt;document&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.digg.com"&gt;&lt;br /&gt;&lt;img src="http://digg.com/img/badges/91x17-digg-button.gif" width="91" height="17" alt="Digg!" /&gt;&lt;br /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-5022515344573618504?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/5022515344573618504/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=5022515344573618504' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/5022515344573618504'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/5022515344573618504'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2007/07/packet-processing-data-lookup-design.html' title='Packet Processor Lookup design'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-7260850907390158855</id><published>2007-07-18T23:39:00.000-07:00</published><updated>2007-07-20T03:20:42.560-07:00</updated><title type='text'>How I sold OO design  to a team of C programmers</title><content type='html'>I was recently faced with the challenge of selling OO design to a group of 'Hardcore C programmers'. Before I could begin, the room was full of - 'What does all that overhead buy us anyways', 'It sure can't be more efficient that C programming' etc. I began by giving them a list of what I thought were the top 5 benefits of OO design -&lt;br /&gt;&lt;br /&gt;Benefits of OO design -&lt;br /&gt;&lt;ol&gt;&lt;li&gt;OO Design is more intuitive (Talk about how we deal with abstractions around us every where in the real world consisting of entities that have both behavior and attributes, give an example of how a person could be 'Abstracted' out to be 'An employee' at work, 'A spouse' at home, a 'Tennis player' at the club etc). Just as we make sense and deal with the world around us through 'Abstractions' it's natural to do so with software design&lt;/li&gt;&lt;li&gt;OO design supports code reuse through - a&gt; Classes b&gt; Inheritance c&gt; Polymorphism and d&gt; Composition. Talk about how OO design supports - 'Reuse of design' as opposed to just 'Reuse of code'. 'Reuse of design' is supported via frameworks that use polymorphism where high level logic 'The Brains' could be embedded in a framework and reused. An example of a 'Socket event loop' could help here. As an aside, you could also talk about 'Templates' and how they enable reuse, but mention that templates aren't part of OO design, but generic design, nevertheless supported by C++.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;OO design leads to flexible software that can be easily modified or extended. This should be the "Clincher". Explain how via OO design one could have 'OLD code call NEW code' through polymorphism and how that would lead to easily extensible code. Also talk about how OO design leads to 'Loose coupling' (Enabled by the technique of Encapsulation) and 'Tight Cohesion' (Enabled by classes), thus leading to code that could be easily modified without causing 'Ripple' or 'Spaghetti' effect on the whole of the application. I gave an example of a Telecom switch software that was first designed for the PSTN (SS7 based) network and later evolved to IP based interfaces very easily, since it was designed to work with abstractions.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;OO Design enables 'Fast prototyping' and an 'Iterative development'. This one should get the program managers interested quickly. Tell them, that in OO design, for the initial prototype of a product, designers need not get bogged down with finding out the best implementation detail, but can go with a simple implementation, suitable abstracted out. At a later stage, this simple implementation could be replaced with a more complex, most optimal one, very easily in a plug-n-play manner.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;OO design allows for late cycle performance tuning. This one should get the 'C' coder who is gunning for you by now, thinking - Aaah! but what about the cost? Explain that software performance tuning is best done after the software is written and tested and the "Hotspots" identified. With OO design, making such late cycle changes will be easy to do, since changes are localized because of loosely coupled code.&lt;/li&gt;&lt;/ol&gt;Once, I was done with that, the amount of resistance dramatically reduced in the room. But then, some chaps were not convinced - 'Hey! But what about code bloat that C++ brings in' they said. I then went on to explain that 'Code bloat' is not caused by C++ as such, but by ignorant programmers and/or bad compilers. I said that if they used a good compiler and stuck to certain principles, they would end up with an executable size equal to their C code, but with a much more elegant body of software. The principles to avoid code bloat that I laid out were -&lt;br /&gt;&lt;br /&gt;To avoid code bloat -&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Disable 'Exception handling' and stick to C style checking of the return codes&lt;/li&gt;&lt;li&gt;Disable RTTI and don't use dynamic_casts, type_id etc&lt;/li&gt;&lt;li&gt;When using templates, use the following techniques&lt;/li&gt;&lt;ol&gt;&lt;li&gt;Move all common code that doesn't depend upon the type to a common base class and use inline template wrappers to get to this implementation. That way, you preserve type safety and automatic code generation of templates, while avoiding code bloat by using a void* based implementation internally&lt;/li&gt;&lt;li&gt;Use 'Template specialization' to avoid cases such as Stack&lt;int&gt;&lt;int&gt; and Stack&lt;long&gt;&lt;long&gt; generating different bodies of code, even though they are binary identical.&lt;br /&gt;&lt;/long&gt;&lt;/int&gt;&lt;/li&gt;&lt;li&gt;Do not inline all template methods. Just because the implementation needs to be in the header, doesn't mean they have to be inline. non-inline method definitions in the header file is allowed for templates&lt;/li&gt;&lt;li&gt;Use a toolchain that offers template optimizations - Eg: Generates only those methods that are ever user in the program, rather than all of them&lt;/li&gt;&lt;/ol&gt;&lt;li&gt;Avoid using iostreams, stick to printfs instead&lt;/li&gt;&lt;/ol&gt;It was either the sheer strain of listening to all this stuff in one helluva long session, or it really made sense. But everyone seemed convinced. Try this at your workplace when someone says - OO design is all hype and doesn't make sense in embedded software products.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.digg.com/"&gt;&lt;br /&gt;&lt;img src="http://digg.com/img/badges/91x17-digg-button.gif" alt="Digg!" height="17" width="91" /&gt;&lt;br /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-7260850907390158855?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/7260850907390158855/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=7260850907390158855' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/7260850907390158855'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/7260850907390158855'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2007/07/selling-oo-design.html' title='How I sold OO design  to a team of C programmers'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1051472106557425450.post-6948628753509275196</id><published>2007-05-03T21:46:00.000-07:00</published><updated>2007-05-03T22:17:18.455-07:00</updated><title type='text'>Token bucket implementation</title><content type='html'>The 'Token bucket' algorithm is commonly used for doing rate limiting of packets in routers. The concept is real simple - A bucket is filled periodically with some tokens equal to the 'Allowed packet rate', each arriving packet consumes available tokens and passes through the bucket. If an arriving packet doesn't find any tokens available when it reaches the router, then obviously the packet rate has exceeded the allowed rate and it has to be dropped. Now, this is real simple. One alteration to this simple scheme is to add 'Burst handling' ability. This is very easily done by setting the 'Bucket size' to some value higher than the packet rate. So, if say 10000 bytes per second is the rate allowed, then if you make the bucket size as 30000, then the tokens can accumulate to 30000 tokens, when the arrival rate is lesser than 10K and these accumulated tokens allow 'Bursts' of up to 30KBps. This evens out between bursty and lean cycles.&lt;br /&gt;&lt;br /&gt;A naive implementation of the token bucket algorithm would be to use timers to fill the buckets with tokens at regular intervals. But, if we need to do per-user rate limiting, this will not be a scalable solution. At each timer pop (Frequent pops) the timer handler will have to walk through so many buckets (1000s of them) and fill the ones that need filling at that particular pop.&lt;br /&gt;&lt;br /&gt;A better solution will not employ timers and will scale easily to any number of token buckets and rates. I have proposed a simple solution &lt;a href="http://docs.google.com/Doc?id=dcvfwzjw_4fvs8q2"&gt;here&lt;/a&gt;. Let me know if you've got any comments or ideas on this.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1051472106557425450-6948628753509275196?l=bapatonline.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bapatonline.blogspot.com/feeds/6948628753509275196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1051472106557425450&amp;postID=6948628753509275196' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/6948628753509275196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1051472106557425450/posts/default/6948628753509275196'/><link rel='alternate' type='text/html' href='http://bapatonline.blogspot.com/2007/05/token-bucket-implementation.html' title='Token bucket implementation'/><author><name>Anil Bapat</name><uri>http://www.blogger.com/profile/02735817359569908460</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://farm1.static.flickr.com/102/305721412_5d17b29f26_t.jpg'/></author><thr:total>2</thr:total></entry></feed>
