C++ for Embedded Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so thank you everybody for coming I'm quite impressed this talk has gathered such a crowd on an afternoon after lunch of the last day so I hope I won't be too boring with you I understand you've kept you've just had lunch maybe I hope that you had coffee to keep yourselves awake like I said I'm quite impressed the number of people who have showed up I remember pitching this talk to a colleague of mine at Intel and he's a kernel developer Linux kernel develops and no I'm not going to accept you I don't want to see anything about C++ I even spoke to Lynn us once about C++ and I don't know if you've ever talked to Lynn us about C++ he's got quite an opinion about it so some of the things I'm going to show are my phone just put it here Mike so nobody ever calls me I never receive any text and apparently this is the first time that happens I was saying Leno's has quite an occasion of C++ he considers many of the things I'm going to show as missed features I consider them as features so let me briefly introduce myself I'm Tiago macieira that is me working with high technology back in 1982 I've been involved with open source development for well over 15 years I'm not going to say exactly how much you most of that time has been actually working with C++ so if any of you were on my talk yesterday when I was talking about ipv6 I started my career in open source by working with ipv6 I was asked what do you want to work on I decided I want to make a browser capable of ipv6 and Mozilla at the time already had support for ipv6 so I started with the KDS browser and you familiar with KDE you know it's written in C++ so that's how I actually ended up working with C++ before then I had access I had submitted a patch to the Linux kernel I had made a couple of modifications working with Pascal with clipper but it wasn't until I started actually doing C++ that I got into my professional career and both C++ and ipv6 actually landed my first job in open-source when I went to work for topic and now I work for Intel as a consequence as evolution of whatever I was doing back then so my job today is to say and convince you that C++ for embedded is possible and it's even a good idea let's quickly let me just take a quick survey of the audience here how many of you program for embedded systems okay I should have asked the other way around and since we're in embedded Linux conference that was to be expected quite dumb of me how many of you do that with C++ you guys can leave I will be reinforcing that idea to you and hopefully I will be giving you more arguments whenever you come to a colleague or a product manager or somebody else who says no no C++ is too big is as a nerve work for embedded for those of you who don't what's your main language of development C right anybody working with PHP okay one yeah so the interesting thing is that I'm working with IOT these days and I see a lot of people doing nodejs or JavaScript I had a colleague presenting earlier this morning about using javascript with nodejs style api to program a microcontroller so working on zephyr i'm not going to pass judgment i think that every we follow the engineers motto here which is the right tool for the right job what I'm trying to show you is C++ is a good tool and will do a good job so I'm Deva I divided my presentation into three sections first I'm going to convince you that C++ is not bad that it actually does work for what we want to do in embedded then I'm going to show some reasons why it's actually better than the competition that being seen and then I want to finish with some of the things that are exclusive to C++ I don't have slides for all the things I'm going to say at the end I'm just going to start rambling about some of the things we started to think about in C++ and I'm here helping to answer questions so feel free to raise your hand if you do not understand me understand most of you are not native English speakers neither am i so feel free to stop me and ask me something so as I said the question we are trying to answer which is the best language for embedded programming I already gave you a hint of what the answer is and it I'm not going to say it's C++ the best and language is the language you're most comfortable with I want you to do a good job in developing systems and especially when we're talking about embedded programming and we're talking now about the embedded programming connected to the Internet also known as IOT you need to be doing a good job these devices need to be updated need to be securely written they need to do their jobs without blowing up every five days because they just use too much memory so the correct answer is use the language that most you know most that you most comfortable with and serves your job so PHP might not work on an embedded system maybe Java won't work because it is the VM takes two Martian garbage collection is not going to help you but one that might is go so you see the little gopher on the top there it is actually a compiled language right I don't know too much about it but I keep hearing from colleagues who do that this is an awesome language and more than that we're here at the embedded Linux conference go because of the way it is developed enforces basically that everything has to be open source but anyway I digress Python and Ruby are good options as well even though they are the interpreted languages they can be byte compiled they have a garbage collection which may not work for everybody the normal options we have are C and C++ again that's what I'm going to be looking into let's start with a couple of facts or myths C++ is more complex than C fact or myth it's a fact so it's actually a fact because yes this standard is bigger I'm going to take a parenthesis here and talk about what the languages are so both C and C++ are developed as part of a distant international standards group ISO what organization ISO they are both standards in that organization and if you want to read the specification you usually go to your national body and you download you buy the specification comes in a book they are quite big this text is very dense it's meant to describe an abstract virtual machine that would reduce something point is the C++ standard is longer and it is complex because it does more stuff that doesn't mean the language is more difficult another one C++ language generates more code and/or requires more RAM myth that is that indeed a myth it does the language is designed around you don't pay for what you don't use right and even some of the language features can be turned off with certain compiler switches so we're going to talk about exceptions later for the moment if you just want to not pay for the cost of exceptions you're writing code that doesn't use exceptions there's a switch that you can pass to your compiler will turn them off and you don't pay for the cost of it right so which options those are so if you're not using exceptions for example GCC you're going to use F no exceptions and this other option that I actually hadn't thought of until I was actually writing the code to figure out the difference in size the synchronous unwind tables and that's interesting because that applies to C as well I was trying to compare the size of a by of a certain function you're going to see later in the presentation between using exceptions and using manual error error detection and error handling in C and it turns out that the C compiler GCC was producing the equivalent information for unwind that is how do you throw an exception through that function which would have meant that if you did that C code would be unsafe because it did not properly handle the case of the exception going through the propagation so if you want to use C++ without exceptions those two options if you in addition to not using exceptions you're neither using type ID or dynamic casting there is an extra option you can use called F know R TTI which stands for runtime type information that turns off the runtime type information most of the time if you're writing compact C++ code you're not going to use you're going to see any difference that one appears when you are besides these two operators when you have polymorphic classes with virtual functions so the moment that you have a virtual function the compiler will emit to the runtime type information for that particular class so you can save a couple of bites by not having it it's going to be about 20 30 bites a / per class and if you're not using this standard library and that's going to be a very big problem for us to discuss you can actually disable the use of the standard library now you can turn completely off because the compiler will still do a couple of things that are required so some of the things that the GCC and G + + and clang do and any other discuss + compiler do is they call out to a couple of methods or you use a couple of symbols from the C++ language support write both GCC and clanks libraries I'm going to see later have sub libraries that contains those symbols so you can use only those and if you install on a regular and this is Linux tribution those actually come as a static library so can easily compile a C++ application that uses nothing more than the C library another myth would be the the C++ language hides things from the compiler and I realized that I did not hide everything in my slide so you know this is supposed to be what the hell is the rest of that was not supposed to happen so this is what happens when you don't so you are viewed all my slides but I never ran them through in presentation mode so the animations I never looked into them so it's a myth you you can hide stuff yes but no more than you can do with macros in C in fact I would would say that the extensiveness of macros because you can't do many things in C so because there are certain things you cannot do in C that you can C++ the use of macros in C++ is smaller in term that means the the converse means that you're using macros a lot more and C and that has sometimes a very very unexpected effect so my rule of thumb whenever you and this is for you if you're compiling code and you see an arrow from the compiler that makes absolutely no sense because it has nothing to do with the code that you're looking at look at the preprocessor output it's very often that somebody defined a macro to one of the words that you use in that line and then the compiler thinks that where you wrote interface and this happens often I have a lot of code in d-bus that uses the word interface that that word should not appear there it's not a valid variable name of course not because windows that age defines it to be struct so you cannot define something like that so be careful we had cases of let me give you another example you're writing a an application that it defines int Sun and Moon can anybody tell me which operating system that will not compile on I'm going to give you a hint one of the companies is named after that so now they no longer exist independently but on Solaris made by Sun Microsystems Sun is defined to one trust me when I compile the code it was the same example ten years ago like why doesn't this call I compile and everybody else and then it kind of didn't compile on the Solaris compiler I started to blame the compiler until I figure that one out so anyway I digress the next question and let's see if the animation works using templates is more expensive so I'm the answer here I'm going to patch my bets and again the admission didn't work it's yes or no so templates by themselves do not imply that you're using more code or more RAM that's a myth it does use more increases the compilation time because the compiler has to do more things and thus uses increases the memory user Java the compiler but by itself they do not cause more code or more RAM to be used however because of the way people currently use templates and the fact that we're trying to produce more optimal code by eliminating function calls eliminating out of line all of this ends up as in line code it does end up increasing the code size it's not a requirement for time place for that to happen but it does happen the way that templates are usually used so both yes or no and the last one in the animations again I'm just going to skip the animation so C++ compilers are not as good as C compilers that's a myth so if you're using any of the major C compilers today they're C++ compilers are equivalently good so both clang GCC Microsoft's Visual Studio compiler the one from Intel of the company a work one they are just as good in C and C++ in Microsoft's case the C++ part is even better because they do follow the C++ standard they do not follow the C standard so for example they'd only brought c99 supports their compiler when C++ started requiring it however they're not as widely supported on embedded platform that's why I'm here so for example I was talking to inés nauseous from the lead architect in in Zephyr and said yeah we don't support C++ nobody has wanted it to do it but if you want to give it a try take a look and I'll probably be doing that just as I mentioned compilers and standard and this will give you a picture what I said before about the libraries so honest a regular Linux system you're going to see either of these two compilers GCC or clang they're more or less equivalently good in G code generation in micro benchmarking I can tell you GCC still generates better code than clang whether that's going to remain for long run not we don't know GCC also does have a couple more aggressive optimizations so we had a problem in queued recently related to the dead store elimination I will not get into the details of it it's just a new switch they added into GCC six and we could not see what was wrong in our code it took somewhat while to identify that yes we were violating some of the requirements of the C++ language and therefore the compiler was right to eliminate some of the code that we did so both clang and GCC are equivalently good GCC comes with its own standard C++ library and they're not very imaginative with the name it's called Lib standard C++ clang because Lib standard C++ was taken just called the Lib C++ it's an L VM library the main difference you're going to see in these two is the license so the GCC one like GCC itself is licensed GPL version 3 with the runtime exception whereas the LLVM code is more often licensed BSD so on many of embedded systems because of the provisions of the GPL for example you might prefer to use a BSD I'm not a lawyer so this is not a legal recommendation it's something that you and your legal team need to come in look into clang has been designed to work with both libraries so it actually does work with both just fine and I've seen many distribution is that default clang to use lib C++ and my answer is don't do that don't try to make that choice on behalf of the user if the user wants to there's a switch that you can pass through the client command line that will switch it from one to the other but you should default to using what everybody else is using on your system which is Lib system C++ in theory GCC should work with Lib C++ I have actually never tested it it's something that I should should be looking into soon because of the what I said about zephyr inside those two libraries like I said there are sub libraries that support the language itself and do not provide the rest of the standard library definition and those would be leaves support C++ and Lib C++ ABI if you take for example on Mac OS what Apple has done is that most of the applications today are linked to Lib C++ and what they've done is that they've replaced Lib C loops names sub C++ they taken it out and they've made libs the C++ ABI a separate shared object so both libraries link to the same same core as a result you can actually run and bloat run applications that link to both libraries at the same time they're compatible among themselves now you can't actually pass structures they defined in one to the other so for example standard string you're not going to be able to pass across the two libraries but exceptions are going to work you're going to be able to call new on one and delete on the other and that just works just fine and that's also because all of this ABI has been defined previous it was actually fine during the Itanium processor apparently some people at intel had too much time on their hands and they decided to do a C++ ABI that was published today it's still known as the ia-64 C++ ABI but it applies to almost all of the processor you see today they depend the made that the normal libraries depend on lips so if we're going to look at embedded systems you're going to have to choose your lib see if you're going to a microcontroller system that gets interesting because you don't have a Lib C with a C API and a POSIX API they're more important than ever you don't have a P thread one so many of the things that exist in the current standard C++ 11 14 and 17 you might not be able to use on very tiny embedded systems but if you're looking at Linux this is just how it works any questions so far okay so let's look into why I think C++ is good let's start with something that bit me when I was working on so I'm the maintainer of a C library called tiny C bore it's meant to run on zephyr I wrote in C because well I just didn't want to have the fight back then so I just wrote it in C it was that simple anyway and just as I was starting with the project I had not configured my compiler with any special switches the following code like this compiles and you can see that in C it prints a warning it doesn't cause an error in C++ is an error and if you're compiling like you just start and you compile the seek this file over there and then work in something else you never realized that this went through with just a warning why is that bad let's suppose that G is a function that takes in 64 this function here passes a 32-bit parameter so G did not take a -1 it took you into max so I've seen many code bases and see where they actually have a couple of W error so they turn a few warnings into error missing prototypes is one of them right so if you're working in C you probably want to make W error equals implicit function Declaration never allow your C code to compile like this in C++ it's not required because it is weak sorry it's not required to make this or an error because it is already an error the language requires that it is that it be defined before there's another reason for that it's because he plus plus supports overloads so it needs to know which of these functions to call the ability to call without a prototype is legacy and C it actually is inherited from be the language came before C so if you didn't know your history yes before C King be in B you only declared you did not declare parameters so that's why in C you just have the parentheses you never have anything inside everything was simply word sized so that would have worked here they didn't have the problem 64 bit but that said we say yeah that's that's just legacy from B this one I find inexcusable so in C casting across different pointer types only triggers awarding like in C++ that's an error so the example here is what happens if I pass a short pointer to an int pointer in a function the C compiler warns but does not stop so this is another one that you might want if you're using C to make an error of in C++ it's an error I simply cannot tell you why this see is not an error I just can't think of any reason why anybody would want to implicitly cast from in short pointer to end pointer this the one below is the dropping of Const that one has more of a reason because the original C language did not have Const so it's a bolted on key word it appeared shortly before C++ was invented or C++ actually came with with Const yeah I mean at least is the right type but if I drop the constant might be right into memory I shouldn't be writing and if this is pointing to read-only memory my application will crash so these are really weird for me working from C bus from C++ to except they are correct in C in other in putting it or the other way around if you program in in C++ your code is safer because you will not commit to these errors the compiler will simply not let you the next one according to Linnaeus is a feature void pointers so you are allowed in C to cast from void into any other pointer without a cast the common the most common use of this is int sharp car pointer something equals malloc you do this very often in C now what happens is that malloc returns why pointer which gets automatically cast to car pointer in C++ that is not allowed and here's why so I have now instead of to have three functions if you look at the bottom most one it's the short pointer again so I'm calling from F I'm calling G and that is okay it costs to void so my short pointer gets to void star but the G function takes that void void star and casts to int star without checking so I can't stop you from casting casting has a place it has a use but let's be sure that you do the right thing in C++ this would have been caught and would have made you think Oh which one was this supposed to be do I really by the way do I really want to do a void star API you if you can't don't you would use templates here but in C++ it is an error and see it's neither an error not even a warning so since we talked about casting I'm just going to plug in this stricter type safety introduces the cast operators here for no other reason that they're easier to grab for so if you need to figure out if you're doing anything wrong if you need to do an audit of your code to make sure that you're casting right things to the right things right you do a grab for static cast all of these it's much easier to find these words which cannot be used in variable names they cannot be pre purposed than to find to write a regular expression that does casting of types and see I would actually say it's impossible to write one the other advantage of having them is that you can't accidentally do more than what you intended so if you're using Const casts the only thing you can do is drop the CV qualifiers CV qualifiers Const and volatile yes but if you're doing a static cast you're not allowed to drop the Const so you can do a couple of things with static cast but not cross barriers that unrelated types and you cannot drop the Const cast so yes in sometimes you see longer code in C that does Const cast to something intermediate and then the static cast to the destination yes it's long-winded but I'm explicitly saying what I'm trying to do I cannot by mistake lose the Const I had code Bay's in cute that was using old-style casts and were C++ we call C casts old-style casts it's actually the the GCC warning for it is w old cast if you do that you try to compile it was trying to remove them it turns out that the Cold Bay is because it was it had been refactored so many times along the way it simply could not be done it simply could not get safety everywhere and get rid of those annoying C casts some functions we're supposed to modify called functions that weren't and then call back into things that were any way from the beginning use them the next topic is just organize your code into classes so I searched for this is G Lib code you can see it's actually from g io it's a GD bus function i was just searching for an example of something short there called a couple of functions inside as you can see this code I hope that everybody in the back and seen by the way the presentation will be available online later so you can look into the code what it does is that it does string nu which allocates something I have no idea why it needs a null that's my opinion on CP on ABI API design why does it need a null there then cause string append string append C while it C is a string why do I need to repeat myself that it is a string so the equivalent code that I just wrote for this with a with the cute classes with cubit array it just calls append now if you're paying attention there are two things different in the C++ code from the C one is more or less evident something is missing on the C++ code the free we're going to get that into later the other one is not as evident and this is something that people actually do complain in C++ because they're say you're hiding stuff can anybody see what is different between the two what I could be hiding so the answer is look at the third upend right it is equivalent to an append see I actually looked into the what what this was supposed to be in a in injeel a bit depending a simple character right so it is the same append function and the reason for that is that we're using overloads in c++ in my opinion they which is not shared by many of the c coders it is actually better to have the overloads here because they do the right thing and i don't have to worry which of these functions are it behaves the same way so if I were to change my arguments I wouldn't have to change the function in fact if this were a Q string instead of Q byte array the code probably would compile anyway and just do the right thing with a cube string instead of Q by 3 even though it doesn't incorporate on characters but on Q car which are you Lee code all right so since we're talking about overloads unimproved functionality in C++ are overloaded so I just copied this from both the sea and the C++ standards on the right you can see the C standard from section 7 12 7 2 so all of chapter 7 in the C standard is the library on the C++ standard since the library so much bigger it is many many character chapters chapter 26 if I'm not mistaken a is the numeric support yeah so it's just showing what we can do with overloads on the right side we have F abs so floating-point absolutes and you have to call F abs F if I want a float based floating point absolute in C++ you have F abs float F abs double and F abs long double you might say well why do I care I'll just call F abs on see why does it matter that it did I have an overload have you ever done floating point emulated in CPU many of you have and working with doubles is more expensive so if I can operate on maybe not this case of F ABS that's actually an easy one just flip one bit but on many operations of using float speeds up the code base so you want to use if your code is only floats you want to keep with floats you don't want to accidentally use double and increase your your runtime because you have to do more precision in C++ we have just call F abs it will call the right one and it even has an abs one which works as integers that allows us to do generic programming now if you ask me when I'm working with C the thing that I miss the most the one thing I would give my castle for if I could just use in C are exactly these structures so I copied this from the font here slightly smaller I was just searching the Linux kernel because gee Lib does not do memory it does not check that malloc succeeded not not going to that so I searched into the kernel of an example of malloc failing came a lock failing and it actually did a couple more things after that right so it's not actually easy so that's a good plus for the kernel they try to avoid doing complex things in functions to avoid exactly this particular problem I know one particular function that is much worse than this it is the one that is called whenever new process starts initially Kait a ton of different things so it has a at least ten different go tools this is a simple one it has to go twos so if you're programming in C very often you will write code like this go-to isn't so bad after all and I'm not passing judgment goog to actually serve a purpose that one is pretty good so what you're seeing here is you do a camera lock and if it failed I'm going to return in Oh ma'am I could have just written here they just use the same one below it is a mod mutex lock a spin lock does some work inside and in the middle of a get a loop that happens inside the path variable might be initialize to false which indicates to know which indicates a problem and in that case it needs to return an error name too long and if that's the case I need to unlock what I had locked and free the memory the working buffer so you have to pair the code like this and if you have more things that might fail in between you have to do more of these so the thing I miss the most in T is resource acquisition is initialization somebody said right that's the full expansion so I invented here some function so a class called pointer holder specifically for the kernel that would do the same thing and then look at I'd actually have mutex lock er not mutex lock and spin lock er not spin lock so I created two extra variables on the stack here automatic variables and as you can see from the rest of the modification that I've done I don't need to remember to unlock them I don't need to remember to free the buffer why is that because the destructors will run right and the destructors will do it for me which could be called hiding it is definitely highly because I don't see it here but in code by code bases where you use a wry resource acquisition is initialization properly and only it it's actually produces safer code so ever since C++ f/11 and more so with C++ 14 there's a trend to get people to stop using raw pointers there are enough in the C++ standard library there are enough classes in the C++ standard library to hold them and manage them for you specifically unique pointer and shared pointer that you almost never need to deal with them directly that means that you you can still link if you try hard enough but it game makes it more difficult the biggest problem you have to face with shared pointers are cyclic reference counting loops so when a depends on B B depends on C C depends on a you might drop all the reference but since they all have a reference to each other they never get destroyed on garbage collection systems the garbage collector actually has to have enough intelligence to do a pass and verify that came back to the original one without increasing the used count and then all of these can actually be thrown away same problem exists in if you're using reference counting I actually immersive the one main criticism I always hear from see people hey there's only a closing record why does the compiler produce so yeah so you're right some people can see will complain that the closing bracket produces a lot of code yes it does right and the worst one is when you get a compiler error in the opening bracket of your class that's another issue yes I've been hiding code right but the interesting thing is there's actually made it more it made it safer I've got a colleague who's a really good C coder he doesn't like C++ at all there's a feature an extension in GCC that tells it whenever this variable goes out go out of goes out of scope called this function so you can actually implement a destructor like functionality with GCC it's not part of the c standard but GCC allows you to do this so we actually started using this in his own code base because as yes it makes it it makes it safer so I'm willing to sacrifice some verbosity some explicitness let's not call it verbosity for the safer code we're going to come back to this again later when we do exceptions in a couple of slides I didn't know exactly what to talk about the containers I was struggling with what to write in this slide so the C++ standard library has a lot of containers it is in my opinion the single most important thing that the C++ standard library has io streams throw them in the garbage the containers are good and more important than that those containers in both Lib C++ and in Lib C++ are written by the same people who wrote your compiler they know how the compiler works that means hopefully that the container is the most optimized possible that could exist for that particular compiler and it often is very hard for you to write something that is as generic and as efficient as the C++ standard compiler standard containers now you can write specialized code that is more efficient yes definitely but to make a generic to work with everything is a little difficult the example I wanted to give and we can draw it later after the session is the normal uses of containers so in a C code base what kind of container is the most often used a linked list linked lists so I was grabbing the G Lib source code and I found they have G array which is an array and I started to figure out where is this used almost nowhere they don't use it on their own code base G list however which is a linked list they use it very often in the C++ world the structure the container that is most often uses some vector which is an array and I can prove to you that a vector of an the same number of elements of the same size is more efficient in many factor so it's faster to search it's faster to iterate over it has less memory overhead so I mean by meaning fewer my locks it uses fewer cache lines and because it's sequential it actually helps the CPU can help you because it knows you're going to usually progress sequentially if you're right aerating over a linked list you might be through going all over the place in memory so the cache lines in the CPU cannot help you by prefetching information that you're going to need a couple of instructions down the line right the problem with them two problems actually one is they're not optimized for code size most often C++ library optimized for performance this is why a lot of these things are in line so yes it's going to produce the fastest possible by the by having as a counter but putting all the code exactly where you used it so it's not calling outside whereas often when we're doing embedded programming we want to actually have shorter code to fit whatever memory we need so the question is if when I mean optimized I mean before or after C++ 11 bill both but more so now with C++ 11 move semantics and and more so they are optimized and the reason again is because all of this ends up in line the code is all generic and ends up in line compilers today both GCC and clang are not as smart enough to do code analysis after code generation and they identify that these blocks of code are actually identical they do it before usually before they expand to assembly and they don't realize that it's actually the same after expanding to assembly so there's room for improving this more especially if you want to do optimization for size just remember one thing parentheses GCC optimizes for size a lot better than clunk does so if you're doing compilation with OS you want GCC not lying today I said there are two problems the other problem that I personally have with the standard containers is that the error reporting they have the only air report they have is exceptions so there has been discussion there have been discussions on the the C++ Committee and stellar mailing list should we have non exceptional code should we support this the people there came to a compromise the answer is like this we believe we meaning C++ standard committee that exceptions are the answer we should be using exceptions and therefore we're going to design our containers and our API around exceptions that said we're going to make it easy for people who don't want to use exceptions to still use some functionality so as an example the upcoming file system API the C++ standard is expected to have as return values standard expected so it's a container that contains that as a parameter the template parameter is your return type might it but it might be actually disengaged containing no value but instead containing an error code that allows you to query it are you an error condition and handle it normally or if you don't and just try to access the value it throws the exception at that point so it is the compromise we found so the people who push and say exceptions are good their argument is yes sitting it looks like it increases code size it looks like an increases code size but that's because we're actually handling all possible errors and not missing anything so I did a test I bless go back to that C group function inside the C the kernel code so I just rewrote it now to use I have the actual complete code for this so the dot dot dot in the middle is I have it I spend some time just drink dummy of everything that called and all these these functions around I just wanted to know the impact of switching from C with the explicit error checking and go twos to using exceptions so there are basically two points in this function where an exception can be thrown the first of them is this K malach right so I didn't change I made I made it's okay my lock is not marked no except so it might throw I wrote the actual pointer holder for a class and the function in between that actually I you can't stay here because he wouldn't be able to read it the function that would have returned the null path I made it a throw or since it's all non inline that basically did not mark it as no except everything else all the other functions especially the ones in the kernel that return void I simply mark them as no except which means that the source the compiler is looking at it say I have two points possible where it's possibly going to throw one of them is before anything happens it's actually in the beginning of the function that this came a lock here so it might just throw and doesn't have to do anything or in between in that dot dot dot there in which case it needs to destroy three objects the two lockers and the pointer holder note that I don't have to return you know man because that's actually an exception thrown but I actually also removed the code that did return e name too long because that's supposedly now an exception so if the function inside said well I can't fit the the name that you wanted me to in the buffer that you passed me I'm going to throw a name too long right so the only return point in this function the normal return is this return zero at the end this one actually means this function did not need to return int it could just have been void because it always succeeds or throws but I actually just realized this when talking to you I could have made this void I compiled it and as an exercise I use the ABI the ABI for the zephyr operating system which I believe is also what the kernel uses inside itself it uses basically passing barring registers as a result the code size grew sixteen bytes and it added the exception table that wasn't needed before and this is where I found that the F Noah synchronous unwind tables was important in C as well because when I had just compiled it in C and had not noticed it you know it did it emit an unwind exception table for C so the difference between the C unwind table that was there and the C++ and wine table with exceptions was 16 bytes only so 0 10 X right 0 X 1 0 the other thing that changed and this is actually where the 16 bytes come from is that the error handling was removed from the main code path really so I'm going to leave you with that I had like I said a couple of more C++ is awesome I'm just going to quickly go through them lambdas I love lambdas it's a good thing to use they can be used in C as well as this example shows the range for I'm going to leave you with this and ask you to spot the error there's an error in the C code trust me unfortunately we're running out of time so I will not give it right now that was my mistake I guess no the error is something different yes I did not divide by size or actually GCC complaints to you and says you're accessing out-of-bounds clan dated where is this the C++ code is much simpler to close there's a bunch of goodies that we're doing in C++ 14 and C++ 17 I don't have the time to explain them all the language is evolving rapidly where C is not so much it's all an ISO standard but interestingly the standard itself can be found on github you can compile it because it stacks they only accept poll requests for editorial changes everything else needs to go through mailing list discussions which we're familiar with so unfortunately I ran out of time thank you so much for coming I'm available in the coffee area afterwards yes and you C++
Info
Channel: The Linux Foundation
Views: 78,983
Rating: 4.9002671 out of 5
Keywords:
Id: wLq-5lBc7x4
Channel Id: undefined
Length: 52min 27sec (3147 seconds)
Published: Tue Apr 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.