Archive for December 7, 2016

Wednesday, December 7, 2016

Swift Optionals and String Interpolation

Ole Begemann:

Do you know this problem? You want to display an optional value in the UI or log it to the console for debugging, but you don’t like the default string conversion for optionals, which results in either "Optional(…)" or "nil".

[…]

The problem with the third option is that the nil-coalescing operator ?? requires matching types — if the left operand is a T?, the right operand must be a T. Applied to the example above, this means I can provide another Int as a default value, but not a string — which is what I’d like to do in this situation.

[…]

I solved this by defining my own custom optional-string-coalescing operator.

Transferring Photos to a New Mac

Becky Hansmeyer:

The next thing I wanted to do was move my Photos and Music libraries over. I dug into my last Time Machine backup and transferred the 90GB Photos library file to my new Mac. However, when I opened Photos, things got…weird. Only a few thumbnails appeared, and Photos refused to let me switch on iCloud Photo Library without purchasing more space, because it was planning to re-upload everything. I went ahead and upgraded to the next storage tier hoping that Photos would check with the server, realize it didn’t need to re-upload everything, and calm the heck down. I was wrong.

So, I closed the program, trashed the library, and started over. This time I switched on iCloud Photo Library to begin with. All of my thumbnails appeared, and Photos started downloading all 11,000 photos and 200+ videos from the cloud. Although not ideal, this was the better option for me because my download speed can top out at 10Mbps while my upload speed is only 0.73Mbps. As of right now, 6 days later, there are ~2,500 left to download.

Still, I can’t believe Photos forces users to either re-upload everything or re-download everything if they use iCloud Photo Library.

If you can’t practically transfer the Photos library, then it sounds like you would have to re-tag all the faces.

Finder Keyboard Shortcut to Show Invisible Files

Quinn Taylor:

macOS 10.12 #protip: Finder supports ⇧⌘. to toggle visibility of hidden files, just like Open/Save panels have for a while.

That’s Shift-Command-Period.

Why Does calloc Exist?

Nathaniel J. Smith (via Hacker News):

So there are lots of books and webpages out there that will claim that the calloc call above is equivalent to calling malloc and then calling memset to fill the memory with zeros[…] So… why does calloc exist, if it’s equivalent to these 2 lines? The C library is not known for its excessive focus on providing convenient shorthands!

[…]

When calloc multiplies count * size, it checks for overflow, and errors out if the multiplication returns a value that can’t fit into a 32- or 64-bit integer (whichever one is relevant for your platform).

[…]

So that’s the first way that calloc cheats: when you call malloc to allocate a large buffer, then probably the memory will come from the operating system and already be zeroed, so there’s no need to call memset. But you don’t know that for sure! Memory allocators are pretty inscrutable. So you have to call memset every time just in case. But calloc lives inside the memory allocator, so it knows whether the memory it’s returning is fresh from the operating system, and if it is then it skips calling memset.

[…]

It turns out that the kernel is also cheating! When we ask it for 1 GiB of memory, it doesn’t actually go out and find that much RAM and write zeros to it and then hand it to our process. Instead, it fakes it, using virtual memory[…]

bluefox:

That’s a nice alternative history fiction.

Here’s an early implementation [that just calls malloc].

LukeShu:

There are several interesting things we learn from poking around V6 though:

  • calloc originated not on UNIX, but as part of Mike Lesk’s “iolib”, which was written to make it easier to write C programs portable across PDP 11 UNIX, Honeywell 6000 GCOS, and IBM 370 OS[0]. Presumably the reason calloc is the-way-it-is is hidden in the history of the implementation for GCOS or IBM 370 OS, not UNIX. Unfortunately, I can’t seem to track down a copy of Bell Labs “Computing Science Technical Report #31”, which seems to be the appropriate reference.
  • calloc predates malloc. As you can see, there was a malloc-like function called just alloc (though there were also several other functions named alloc that allocated things other than memory)

ksherlock:

OpenBSD added calloc overflow checking on July 29th, 2002. glibc added calloc overflow checking on August 1, 2002. Probably not a coincidence. I’m going to say nobody checked for overflow prior to the August 2002 security advisory.

mnay:

It is not only a security flaw but also violation of C Standards (even the first version ratified in 1989, usually referred to as C89). […] So if it cannot allocate space for an array of nmemb objects, each of whose size is size, then it has to return null pointer.

nicolast:

And then there’s of course when calloc returns non-zeroed memory once in a while, which causes... ‘interesting’ bugs.